mvc3 RedirectToAction

时间:2011-06-29 18:48:16

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

为什么这不起作用。当我在Create方法中重定向时,它一直告诉我编辑需要EditStudyModel。重定向应该做一个get,对吧?

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

2 个答案:

答案 0 :(得分:1)

您将返回带有URL作为字符串的重定向,控制器无法解析GUID并将其转换回guid对象,因此无法解析要使用的正确方法签名。将其更改为:

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

public ActionResult Edit(string scientificStudyId)

答案 1 :(得分:0)

我发现了这个问题。我复制粘贴我的create.cshtml到edit.cshtml忘了更改第一行: @model Website.Models.CreateStudyModel - >到 - > @model Website.Models.EditStudyModel