简体   繁体   中英

Update data in Partial View

I'm new to Asp.Net MVC and I can't figure out how to update data on post for a Partial View. I don't have a problem with the GET and displaying the data in the partial view.

I'm not sure where to put the post code for the partial view data....in the post method for the parent page? or a post method for the partial view?

When I run the code below I get this message when I submit.

"A public action method 'ScoreRelease' was not found on controller 'Registration.Web.Controllers.AgreementsController'."}

It finds the controller on the initial page load, but not when I call return View("Review"); in the post method.

Parial View called from "Review" page

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

ScoreRelease Partial View

@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>


}

Review Controller

    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);
    }

将Html.BeginForm与参数一起使用:

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

You have to put Post Method in Partial View. You can do it two way either Html.BeginForm() or Ajax.BeginForm . If you are display this Partial View in Popup window then better use it as Ajax. Whatever Action you put in view you have to make same method name with [httppost] tag in controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM