传递给操作的空且始终有效的模型
怎么了?模型始终为空。
控制器
[HttpGet]
public ViewResult Add()
{
return View("Edit");
}
[HttpPost]
public ViewResult Add(Models.EditViewModel m)
{
if (ModelState.IsValid)
{
return View("Success");
}
else
{
return View("Edit", m);
}
}
模型:
public class EditViewModel
{
[Required]
[Display(Name = "JobTitle", ResourceType = typeof(DisplayNames))]
public string Title { get; set; }
[Required]
[Display(Name = "JobDescription", ResourceType = typeof(DisplayNames))]
public string Description { get; set; }
}
视图
@model MyWebSite.Models.EditViewModel
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true, "Please correct the errors and try again.")
@Html.EditorForModel()
<input type="submit" value="Send" />
}
渲染视图
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<form action="/MyAction" method="post"><div class="editor-label"><label for="Title">Função</label></div>
<div class="editor-field"><input class="text-box single-line" data-val="true" data-val-required="Informe um valor para Fun&#231;&#227;o" id="Title" name="Title" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="Title" data-valmsg-replace="true"></span></div>
<div class="editor-label"><label for="Description">Descrição</label></div>
<div class="editor-field"><input class="text-box single-line" data-val="true" data-val-required="Informe um valor para Descri&#231;&#227;o" id="Description" name="Description" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="Description" data-valmsg-replace="true"></span></div>
<input type="submit" value="Send" />
</form>
What is wrong? Model is always null.
Controller
[HttpGet]
public ViewResult Add()
{
return View("Edit");
}
[HttpPost]
public ViewResult Add(Models.EditViewModel m)
{
if (ModelState.IsValid)
{
return View("Success");
}
else
{
return View("Edit", m);
}
}
Model:
public class EditViewModel
{
[Required]
[Display(Name = "JobTitle", ResourceType = typeof(DisplayNames))]
public string Title { get; set; }
[Required]
[Display(Name = "JobDescription", ResourceType = typeof(DisplayNames))]
public string Description { get; set; }
}
View
@model MyWebSite.Models.EditViewModel
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true, "Please correct the errors and try again.")
@Html.EditorForModel()
<input type="submit" value="Send" />
}
Rendered View
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<form action="/MyAction" method="post"><div class="editor-label"><label for="Title">Função</label></div>
<div class="editor-field"><input class="text-box single-line" data-val="true" data-val-required="Informe um valor para Função" id="Title" name="Title" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="Title" data-valmsg-replace="true"></span></div>
<div class="editor-label"><label for="Description">Descrição</label></div>
<div class="editor-field"><input class="text-box single-line" data-val="true" data-val-required="Informe um valor para Descrição" id="Description" name="Description" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="Description" data-valmsg-replace="true"></span></div>
<input type="submit" value="Send" />
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
知道了!
问题是一条糟糕的路线,每次都会创建一个新模型。
更改
更改为
Got it!
The problem was a bad route that was creating a new model everytime.
Changed
To