【发布时间】: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