每当调用控制器中的HttpGet方法时,它就会生成局部视图。
控制器HttpGet方法
[HttpGet]
public ActionResult AddCredit(Guid creditBalanceId)
{
var newCredit = new AddCredits()
{
CreditBalanceId = creditBalanceId
};
return PartialView(newCredit);
}
查看
@model AdminPortal.Areas.Customer.Models.ViewModels.AddCredits
@Html.HiddenFor(m=>m.CreditBalanceId)
<div class="input-small" id="credit">@Html.EditorFor(m=>m.CreditToAdd) </div>
@Html.ActionLink("Add","AddCredit", new {@class="btn"})
每当在局部视图中点击添加按钮时,我希望它通过HiddenFor( CreditBalanceId )和 CreditToAdd <转发到我的控制器的 HttpPost方法 / strong>值
[HttpPost]
public ActionResult AddCredit(AddCredits credits)
{
_businessUnitRepository.AddCredits(credits);
Information("Credits Successfully added!");
return RedirectToAction("LicenseDetails");
}
问题
我需要对视图进行哪些更改,以便在单击按钮时,我会转到控制器中带有所有值的httppost方法?
答案 0 :(得分:1)
你试过这个吗?
@using(Html.BeginForm("Add", "AddCredit"))
{
@Html.HiddenFor(m=>m.CreditBalanceId)
<div class="input-small" id="credit">@Html.EditorFor(m=>m.CreditToAdd) </div>
<button type="submit" class="btn">Add Credit</button>
}
答案 1 :(得分:1)
您可以使用Html.BeginForm和一种提交方式轻松完成。这个链接是一个很好的例子MVC4 forms that save to entity framework models