简体   繁体   中英

mvc3 RedirectToAction

Why isn't this working. It keeps telling me edit needs a EditStudyModel when I redirect in my Create method. A Redirect should do a get, right?

    public ViewResult Create()
    {
        var createStudyModel = new CreateStudyModel();
        return View(createStudyModel);
    }

    [HttpPost]
    public ActionResult Create(CreateStudyModel createStudyModel)
    {
        try
        {
            //TODO: Send CreateStudyCommand
            return RedirectToAction("Edit", new { scientificStudyId = new Guid("{1C965285-788A-4B67-9894-3D0D46949F11}") });
        }
        catch
        {
            return View(createStudyModel);
        }
    }

    [GET("ScientificStudy/Create/{scientificStudyId}")]
    public ActionResult Edit(Guid scientificStudyId)
    {
        //TODO: Query real model
        var model = new EditStudyModel() {StudyNr = "T01", StudyName = "Test"};
        return View(model);
    }

    [HttpPost]
    public ActionResult Edit(EditStudyModel editStudyModel)
    {
        try
        {
            //TODO: Send UpdateStudyCommand
            return RedirectToAction(""); //TODO: Terug naar Studie lijst
        }
        catch
        {
            return View(editStudyModel);
        }
    }

You're returning a redirect with a URL as a string, the controller isn't able to parse the GUID and convert it back to a guid object, so it's not able to resolve the correct method signature to use. Change it to:

return RedirectToAction("Edit", new { scientificStudyId = "{1C965285-788A-4B67-9894-3D0D46949F11}" });

and

public ActionResult Edit(string scientificStudyId)

I found the issue. I copy paste my create.cshtml to edit.cshtml Forgot to change the first line: @model Website.Models.CreateStudyModel --> to --> @model Website.Models.EditStudyModel

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