我正在使用ASPNET MVC 5来开发一个Web应用程序,我有一个带有两种形式的强类型视图,第二个是在第一个被提交时(通过ajax)显示:
a.thumbnail:hover,
a.thumbnail:focus,
a.thumbnail.active
{
border-color: gray;
color:#003300;
background-color:#ADDEA
}
在控制器上,第二种形式的操作:
@model DTO.UserDTO
<form id="firstForm">
@Html.EditorFor(model => model.Name) //need this property on both forms, in the second form just readonly with this input value.
@Html.ValidationMessageFor(model => model.Name, string.Empty})
<button type="submit" id="btnSearchName"></button>
</form>
//second form
@using (Html.BeginForm())
{
//other inputs with other properties in the UserDTO viewmodel
<input type="submit" class="btn btn-success btn-lg" value="Save" />
}
<script>
$("#firstForm").submit(function (e) {
e.preventDefault();
if (!$(this).valid()) {
return false;
}
findName(); //ajax request
return true; // if true, expand(show) the second form
});
</script>
所以,问题是我需要将 [HttpPost]
public ActionResult Save([Bind(Exclude = "Id")]UserDTO model)
{
//the modelstate fails here because the Name is within the Required Attribute and its on the first form.
}
属性传递给用户在第一个表单上输入的实际值内的Name
操作,如何添加&#39;反映& #39;第二种形式的Save
属性呢?
感谢。
答案 0 :(得分:1)
我认为用户可能会更改name
控件中的值,您不需要在第二种形式中更改它,并且您只需要将其与其数据一起发送。在这种情况下,您需要第二种形式的隐藏输入,其中将存储name
属性的值:
<form id="firstForm">
@Html.EditorFor(model => model.Name, new { id = "yourNameInput" }) //need this property on both forms, in the second form just readonly with this input value.
@Html.ValidationMessageFor(model => model.Name, string.Empty})
<button type="submit" id="btnSearchName"></button>
</form>
//second form
@using (Html.BeginForm())
{
@Html.HiddenFor(model => model.Name, new { id = "yourHiddenInput" })
//other inputs with other properties in the UserDTO viewmodel
<input type="submit" class="btn btn-success btn-lg" value="Save" />
}
提交第一个表单时,请复制第二个表单隐藏字段中name
属性的值:
<script>
$("#firstForm").submit(function (e) {
e.preventDefault();
if (!$(this).valid()) {
return false;
}
$("#yourHiddenInput").val($("#yourNameInput").val());
findName(); //ajax request
return true; // if true, expand(show) the second form
});
</script>