【问题标题】:How can i pass complex object from view to controller with POST - ASP.NET MVC如何使用 POST - ASP.NET MVC 将复杂对象从视图传递到控制器
【发布时间】:2014-10-22 07:51:02
【问题描述】:

您好,我试图找到解决问题的方法,但我找不到。我有一门有一篇论文和很多关键词的课

public class ThesisWithKeywordsModel
    {
        public Thesis thesis { get; set; }
        public IQueryable<Keyword> keywords { get; set; }
        public IEnumerable<int> checkboxList { get; set; }
    }

动作

        [HttpGet]
        public ActionResult CreateThesis()
        {
            ThesisWithKeywordsModel thesis = new ThesisWithKeywordsModel();
            thesis.thesis = new Thesis();
            thesis.keywords = db.KeywordRepository.GetAllKeywords();
            thesis.checkboxList = new List<int>();
            return View(thesis);
        }

我使用 thesis.keywords 来显示复选框按钮列表,并放置 thesis.checkboxList 来保存来自视图的返回结果,该视图必须是 ICollection(int) id选中的复选框按钮。

这是我的观点

@model SearchSystem.Models.ThesisWithKeywordsModel

@{
    var listField = new List<SelectListItem>();
    listField.Add(new SelectListItem() { Text = "Софтуер", Value = "Софтуер", Selected = true });
    listField.Add(new SelectListItem() { Text = "Хардуер", Value = "Хардуер" });
    listField.Add(new SelectListItem() { Text = "Мрежи", Value = "Мрежи" });

    var listFree = new List<SelectListItem>();
    listFree.Add(new SelectListItem() { Text = "Свободна", Value = "Свободна", Selected = true });
    listFree.Add(new SelectListItem() { Text = "Заета", Value = "Заета" });
}

@using (Html.BeginForm("CreateThesis", "ProfessorProfile", FormMethod.Post))
{
     <table class="table">
        <tr>
            <td>@Html.LabelFor(x => x.thesis.ThesisTitle)</td>
            <td>@Html.EditorFor(x => x.thesis.ThesisTitle)</td>  
        </tr>
        <tr>
             <td>@Html.LabelFor(x => x.thesis.ThesisDescription)</td>
             <td>@Html.EditorFor(x => x.thesis.ThesisDescription)</td>
        </tr>
        <tr>
             <td>@Html.LabelFor(x => x.thesis.Field)</td>
             <td>@Html.DropDownList("Field", listField)</td>
        </tr>
         <!--<tr>
             <td>@Html.LabelFor(x => x.thesis.Free)</td>
             <td>@Html.DropDownList("Free", listFree)</td>
         </tr>-->
    </table>

    foreach(var keyword in Model.keywords)
    {
        <input type="checkbox" id="@keyword.KeywordId" name="checkboxList" value="@keyword.KeywordId">
        <label for="@keyword.KeywordId">@keyword.Value</label><br/>
    }

    <input type="submit" value="Create"/>
}

这是我的另一个动作

[HttpPost]
        public ActionResult CreateThesis(ThesisWithKeywordsModel thesis)
        {
            if (ModelState.IsValid)
            {
                var loggedProfessorId = db.ProfessorRepository.GetProfessorIDByUserName(User.Identity.Name);
                if (loggedProfessorId != 0)
                {
                    var professor = db.ProfessorRepository.GetProfessorById(loggedProfessorId);
                    db.ProfessorRepository.AddThesis(professor, thesis.thesis);
                    return RedirectToAction("MyTheses");
                }
                return RedirectToAction("Login", "Account");
            }
            return View(thesis);
        }

问题是对象ThesisWithKeywordsModel thesis 为空,thesis.thesis、thesis.checkboxList 均为空。有趣的是,当我在响应正文中看到响应时,我有 POST 参数,如果我将 ThesisWithKeywordsModel thesis 更改为 Thesis thesis,我将拥有我从表单发布的所有值。 p>

【问题讨论】:

  • 从参数名称“论文”更改为其他名称后尝试
  • 我不认为它来自名字,因为如果我使用论文论文,我会得到所有的论文。一切都可以从 antion 传递到控制器,但在那之后......也许我必须以其他方式返回结果,但我不知道如何。这就是我在请求 thesis.ThesisTitle:test ' thesis.ThesisDescription:test checkboxList:1 checkboxList:2 ' 中传递的内容 - 一切都是空的
  • 您混淆了ModelBinder,因为操作方法中的参数名称与模型中的属性名称相同。将方法更改为public ActionResult CreateThesis(ThesisWithKeywordsModel model),它应该正确绑定
  • @Stephen Muecke,这就是我的建议,但似乎 Trifon Dardzhonov 没有尝试过
  • 哇,它有帮助 :) 我是 asp mvc 的新手,我花了一整天的时间来解决这个愚蠢的问题 :D 很多很多很多谢谢 :)

标签: c# asp.net asp.net-mvc view parameter-passing


【解决方案1】:

首先,我不会尝试将 IQueryable 用于您的 Keywords 属性,最好使用 IList。其次,将关键字和 CheckboxList 组合到一个属性中可能会更好,让您的关键字类如下所示:

public class Keyword
{
    public int KeywordId {get;set;}
    public bool Checked {get;set;}
}

所以ThesisWithKeywordsModel 看起来像这样:

public class ThesisWithKeywordsModel
{
    public Thesis thesis { get; set; }
    public IList<Keyword> keywords { get; set; }
}

然后在你的控制器中,你应该能够做到:

thesis.keywords = db.KeywordRepository.GetAllKeywords()
                     .Select(kw => new Keyword
                     {
                        KeyeordId = kw.KeywordId,
                        Checked = false
                     }).ToList();

接下来,当向视图传递值列表或从视图传递值列表时,您需要将 cshtml 中的 foreach 循环更改为 for 循环。

for(var i = 0; i < Model.keywords.Count; i++)
{
    <input type="checkbox" id="@Model.keywords[i].KeywordId" name="checkboxList" checked='@Model.keywords[i].Checked ? "checked" : ""'>
    <label for="@Model.keywords[i].KeywordId">@Model.keywords[i].Value</label><br/>
}

【讨论】:

    猜你喜欢
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多