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

本文关键字:视图 控制器 对象 复杂 POST-ASP 何使用 NET MVC | 更新日期: 2023-09-27 18:19:40

嗨,我试图找到解决问题的方法,但我做不到。我有一节课,有一篇论文和许多关键词

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);
        }

其中,我使用论文.关键字来显示复选框按钮的列表,并使用论文.复选框列表保存视图中返回的结果,这些结果必须是选中复选框按钮中的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为null,thesis.thesis和thesis.checkboxList均为null。有趣的是,当我在响应体中看到响应时,我有POST参数,如果我用主题论文更改ThesisWithKeywordsModel thesis,我就有了从表单发布的所有值。

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

首先,我不会尝试对Keywords属性使用IQueryable,最好使用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/>
}