如何使用POST - ASP.NET MVC将复杂对象从视图传递到控制器

时间:2014-08-28 12:35:00

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

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

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&# 39;来自选中的复选框按钮。

这是我的观点

@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论文为null,论文.thesis,thesis.checkboxList都为null。有趣的是,当我在响应主体中看到响应时,我有POST参数,如果我用论文论文更改 ThesisWithKeywordsModel论文,我拥有从表单发布的所有值。

1 个答案:

答案 0 :(得分:1)

首先,我不会尝试将IQueryable用于Keywords属性,更好的方法是使用IList。其次,将Keywords和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/>
}