我在 MakePayment.cshmtl View 的模型中获取数据时遇到问题。
AccountScreen.cshtml 正在调用 MakePayment.cshtml View :
@model SuburbanCustPortal.SuburbanService.CustomerData
@{
ViewBag.Title = "Account Screen";
}
<h2>AccountScreen</h2>
<div class="leftdiv">
<fieldset>
<legend>customer info</legend>
@Html.Partial("CustomerInfoPartialView", Model)
</fieldset>
<fieldset>
<legend>delivery address</legend>
@Html.Partial("DeliveryAddressPartialView", Model)
</fieldset>
<fieldset>
<legend>delivery info</legend>
@Html.Partial("DeliveryInfoPartialView", Model)
</fieldset>
</div>
<div class="rightdiv">
<fieldset>
<legend>balance</legend>
<div>
@Html.Partial("BalancePartialView", Model)
</div>
</fieldset>
<fieldset>
<legend>payment</legend>
<div>
@Html.Partial("MakePayment", Model)
</div>
</fieldset>
<fieldset>
<legend>billing info</legend>
<div>
@Html.Partial("BillingInfoPartialView", Model)
</div>
</fieldset>
</div>
我的 MakePayment.cshtml View :
@model SuburbanCustPortal.SuburbanService.CustomerData
@using (Html.BeginForm("MakePayment2", "Customer", FormMethod.Post))
{
<div style="text-align:center;">
<input class="makePaymentInput" type="submit" value="Make a Payment" />
</div>
}
我的客户 Controller :
public ActionResult AccountScreen(LogOnModel model)
{
return ShowCustomer(model.AccountNumber);
}
public ActionResult MakePayment(CustomerData model)
{
return View("MakePayment", model);
}
[HttpPost]
public ActionResult MakePayment2(CustomerData model)
{
//CustomerData model = new CustomerData();
var newmodel = new PaymentModel.SendToGateway();
newmodel.AccountBalance = model.TotalBalance;
newmodel.Amount = model.TotalBalance;
return RedirectToAction("PrePayment", "Payment", newmodel);
}
永远不会到达public ActionResult MakePayment(CustomerData 模型)。
我的问题:正在访问[HttpPost] public ActionResult MakePayment2(CustomerData model),但模型中有空值。
我知道来自 AccountScreen 的数据初始模型正在被填充,因为正在呈现的其他 View 正在显示数据。
有人看到我做错了什么吗?
请您参考如下方法:
问题是您的表单中除了提交按钮外什么也没有。您需要确保存在输入字段(文本框、选择列表或隐藏字段),因为这些是将数据发送回 Controller 的内容。
您可以尝试使用 EditorForModel在你的局部 View 中:
@using (Html.BeginForm("MakePayment2", "Customer", FormMethod.Post))
{
@Html.EditorForModel()
<div style="text-align:center;">
<input class="makePaymentInput" type="submit" value="Make a Payment" />
</div>
}
根据评论编辑
Razor 不包含 Html.HiddenForModel()方法,无论出于何种原因。可能的解决方法:
- 使用
Html.HiddenFor(model => model.Property)列出模型的每个属性 - Annotate the model properties with
\[HiddenInput\] - 使用
EditorForModel()但将其包裹在<div style="display: none;"></div>中(请注意,恶意用户仍然可以修改属性,就好像它们是可见的一样。) - Use only
Html.HiddenFor(model => model.id)and fetch the model in the controller. - 使用MVC Futures assembly中的序列化方法
相关问题在这里:
