简体   繁体   English

如何通过列表<mycustomclass>从controller来看如果不是表格?</mycustomclass>

[英]How to pass List<MyCustomClass> from view to controller if it is not a form?

I have a Search page that is not a form, and it is simplified to the following.我有一个不是表单的搜索页面,它被简化为以下内容。

My model is as follows:我的 model 如下:

public class SearchCategory
{
    public string Category { get; set; }
}
public class SearchViewModel
{
    public string Category { get; set; }
    public List<SearchCategory> Categories { get; set; }
}

In my view, my ActionLink is as follows:在我看来,我的 ActionLink 如下:

@Html.ActionLink("Displaytext", "Search", "Explore", new { category = "SomeValue", categories = Model.Categories }, null)

My Controller's ActionResult is as follows:我的 Controller 的 ActionResult 如下:

public ActionResult Search(string category, List<SearchCategory> categories)
{
    SearchViewModel SearchModel = new SearchViewModel();
    if (category == null) { SearchModel.Category = "All Entries"; } else { SearchModel.Category = category; }

    if (SearchModel.Category == "All Entries")
    {
        SearchModel.Categories = new List<SearchCategory>();
    }
    else
    {
        SearchModel.Categories = categories;
    }

    SearchCategory c = new SearchCategory();
    c.Category = SearchModel.Category;
    if (SearchModel.Category != "All Entries")
    {
        if (SearchModel.Categories.Contains(c))
        {
            //do nothing, because the categories list already contains this category
        }
        else
        {
            SearchModel.Categories.Add(c);
        }
    }
    return View(SearchModel);
}

Within my controller's ActionResult , I am adding items to the categories list, and I am able to see this as a populated list if I put a breakpoint on the ActionLink when it re-enters the view.在我的控制器的ActionResult中,我将项目添加到categories列表中,如果我在ActionLink重新进入视图时在其上放置断点,我可以将其视为填充列表。 My problem is that the ActionResult receives a null instead of the populated list passed from the view when it re-enters the controller.我的问题是,当它重新进入 controller 时, ActionResult接收到 null 而不是从视图传递的填充列表。

What am I doing wrong?我究竟做错了什么? Do I absolutely need to set this search page up as a form, or is there some way to get the view to pass the populated list back to the controller?我是否绝对需要将此搜索页面设置为表单,或者有什么方法可以让视图将填充的列表传递回 controller?

Good question... you have to deal with strings (not a class) cause ASP.NET doesn't convert it to standard HTTP GET Request好问题...您必须处理字符串(不是类),因为 ASP.NET 不会将其转换为标准 HTTP GET 请求

I made an example.我做了一个例子。 Notice this "categories.categories[@i].Category"注意这个“categories.categories[@i].Category” 节目 classes班级

public class SearchCategory
{
    public string Category { get; set; }
}
    public class SearchViewModel
{
    public List<SearchCategory> Categories { get; set; }
}

Here is my Method这是我的方法

        public ActionResult Search(string category, SearchViewModel categories)
    {
        if (categories == null) categories = new SearchViewModel { };
        if (categories.Categories == null) categories.Categories = new List<SearchCategory>();
        if (category != null) categories.Categories.Add(new SearchCategory { Category = category }); // add category to the list
        return View(categories);
    }

Here is View这是视图

 @model WebApplication6.Models.SearchViewModel @using WebApplication6.Models; @{ ViewBag.Title = "Index"; } <form> @{ int i = 0; if (Model.Categories.= null) { foreach (var item in Model.Categories) { <input type="hidden" name="categories.categories[@i].Category" value="@item;Category" /> i++. } } <input type="hidden" name="category" value="Abdallah Hiekal" /> <input type="submit" name="Click me." /> } </form> <.--print--> @if (Model.Categories.= null) { foreach (var item in Model.Categories) { <div> @item.Category <hr /> </div> } } <!--end print--> <h2>Index</h2> @Model.Categories.Count()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM