【问题标题】:Forwarding the partial view's details to HttpPost method of a controller when button in partial view is clicked单击部分视图中的按钮时,将部分视图的详细信息转发到控制器的 HttpPost 方法
【发布时间】:2013-08-16 08:24:43
【问题描述】:

每当调用控制器中的 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) 和 CreditToAddHttpPost 方法 /strong>价值

[HttpPost]
public ActionResult AddCredit(AddCredits credits)
{
    _businessUnitRepository.AddCredits(credits);
    Information("Credits Successfully added!");
    return RedirectToAction("LicenseDetails");
}

问题

我需要对视图进行哪些更改,以便在单击按钮时将所有值转发到控制器中的 httppost 方法?

【问题讨论】:

    标签: html asp.net-mvc http asp.net-mvc-4 view


    【解决方案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>
    }
    

    【讨论】:

    • 没有尝试过,但这是我要感谢的选项。无论如何,谢谢你再次拯救我的一天
    【解决方案2】:

    您可以使用 Html.BeginForm 之类的内容和提交类型轻松完成。这个链接是一个很好的例子MVC4 forms that save to entity framework models

    【讨论】:

      猜你喜欢
      • 2016-03-21
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多