![](/img/trans.png)
[英]How can I pass an object from my view to my controller in ASP.NET MVC?
[英]How can i pass complex object from view to controller with POST - ASP.NET MVC
嗨,我试图找到解决我的问题的方法,但我不能。 我有一堂课,有一个论文,有很多关键词
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为null,thesis.thesis,thesis.checkboxList都为null。 有趣的是,当我在响应正文中看到响应时,我具有POST参数,并且如果我将ThesisWithKeywordsModel论文更改为Thesis论文,则我拥有从表单中发布的所有值。
首先,我不会尝试将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();
接下来for
在将值列表传递到视图或从视图传递值时,需要将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/>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.