我的问题有点奇怪。所以在Index.cshtml中我填充数据,在某些地方我使用
@Html.Action("_Create")
创建一个partialView,其中我填充了来自服务器的一些数据,以及需要完成提交的一些空字段。
[HttpGet]
public PartialViewResult _Create()
{
var userOrder = _orderRepository.CurrentUser(User.Identity.GetUserName());
var something = new CurrentListOrderViewModel()
{
PersonName = userOrder.Select(p=>p.PersonName).FirstOrDefault(),
Funds = userOrder.Select(p=>p.Funds).FirstOrDefault(),
Order = "",
OrderCosts = 0,
Restaurant = "",
TodayOrderDate = DateTime.Now,
WindowsName = User.Identity.GetUserName()
};
return PartialView(something);
}
[HttpPost]
[ValidateAntiForgeryToken]
public PartialViewResult _Create([Bind(Include = "PersonName,Funds,Order,OrderCosts,WindowsName,TodayOrderDate")]CurrentListOrderViewModel model)
{
if(ModelState.IsValid)
{
return PartialView();
}
return PartialView(model);
}
所以问题是当页面加载HTTPGET时一切正常,表格从数据库中填充,如果我将_Create.cshtml中的@ Html.EditorFor更改为其他内容以便我可以禁用它,我会丢失数据。可能是什么原因?我真的不明白。
@model FoodOrderQubizInterfaces.ViewModel.Order.CurrentListOrderViewModel
@using(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<tr>
<td>
@Html.EditorFor(model => model.PersonName)
@Html.ValidationMessageFor(model => model.PersonName)
</td>
<td>
@Html.EditorFor(model => model.Funds, new { htmlAttributes = new { disabled = "disabled" }, })
@Html.ValidationMessageFor(model => model.Funds)
</td>
<td>
@Html.EditorFor(model => model.Order)
@Html.ValidationMessageFor(model => model.Order)
</td>
<td>
@*@Html.DropDownListFor(model => model.Restaurant,*@
<select>
<option value="1">Bigys</option>
<option value="2">Zulu</option>
</select>
</td>
<td>
@Html.EditorFor(model => model.OrderCosts)
@Html.ValidationMessageFor(model => model.OrderCosts)
</td>
<td>
@Html.EditorFor(model => model.WindowsName)
@Html.ValidationMessageFor(model => model.WindowsName)
<input type="submit" value="Save/Modify" class="btn btn-default" />
</td>
<td>
@Html.EditorFor(model => model.TodayOrderDate)
@Html.ValidationMessageFor(model => model.TodayOrderDate)
</td>
<td>
</td>
</tr>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
希望你们可以帮助我一些建议,因为我没有想法
答案 0 :(得分:0)
我在搜索网页一段时间后发现了这个问题。原因是当您提交并且添加了禁用属性时,它将不会添加到模型中,因为他认为模型已禁用,因此用户无法与之交互。
所以要修复它我从
改变了<td>
@Html.EditorFor(model => model.Funds, new { htmlAttributes = new { disabled = "disabled" }, })
@Html.ValidationMessageFor(model => model.Funds)
</td>
到
@Html.TextBoxFor(model => model.Funds, new { @readonly = true })
那就是保存数据。我希望它可以帮助其他人解决我的问题