【问题标题】:Update data in Partial View在局部视图中更新数据
【发布时间】:2012-08-06 18:58:57
【问题描述】:

我是 Asp.Net MVC 的新手,我不知道如何更新部分视图的帖子数据。我对 GET 和在部分视图中显示数据没有问题。

我不确定将部分视图数据的 post 代码放在哪里......在父页面的 post 方法中?还是局部视图的 post 方法?

当我运行下面的代码时,我会在提交时收到此消息。

“在控制器 'Registration.Web.Controllers.AgreementsController' 上找不到公共操作方法 'ScoreRelease'。”}

它在初始页面加载时找到控制器,但在我调用 return View("Review");在 post 方法中。

从“Review”页面调用的Parial View

 @{Html.RenderAction("ScoreRelease", "Agreements");}

ScoreRelease 部分视图

@model Registration.Web.Models.ReviewModel.ReleaseScore
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    
    <div class='group' id='data_release'>
        <h4>
            Data Release
        </h4>
        <p>
            Do you wish to release your scores?
        </p>
        <ul class='input_group'>
            <li>
                @Html.RadioButtonFor(model => model.ReleaseScoreIndicator, true)
                <label>
                    Yes
                </label>
            </li>
            <li>
                @Html.RadioButtonFor(model => model.ReleaseScoreIndicator, false)
                <label>
                    No
                </label>
            </li>
        </ul>
          <input type="submit" value="Save"  />
    </div>

   
}

审核控制器

    public ActionResult Review()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Review(ReviewModel.ReleaseScore model)
    {
        var agmtsService = new AgreementsService();
        agmtsService.UpdateReleaseScoreIndicator(model.ReleaseScoreIndicator);

        return View("Review");

    }

    [HttpGet]
    public ActionResult ScoreRelease()
    {
        var agmtsService = new AgreementsService();
        bool scoreRelease = agmtsService.GetReleaseScoreIndicator();

        var vm = new ReviewModel.ReleaseScore();
        vm.ReleaseScoreIndicator = scoreRelease;

        return PartialView(vm);
    }

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-partialview


    【解决方案1】:

    使用带参数的 Html.BeginForm:

    @using (Html.BeginForm("Action", "Controller", FormMethod.Post)) 
    

    【讨论】:

      【解决方案2】:

      您必须将 Post Method 放在 Partial View 中。您可以通过Html.BeginForm()Ajax.BeginForm 两种方式进行操作。如果您在弹出窗口中显示此部分视图,则最好将其用作 Ajax。无论您在视图中放置什么 Action,您都必须在控制器中使用 [httppost] 标签创建相同的方法名称。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-07
        • 1970-01-01
        • 2017-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多