0

我需要使用 Post-Redirect-Get 来显示我在“索引”页面的“信息”页面中输入的数据。我有以下方法,但它不起作用。它甚至不会在提交时重定向我。我究竟做错了什么?

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

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

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

    //Get info
    [HttpGet]
    public ActionResult Submit(Models.Information FirstName,
                               Models.Information LastName,
                               Models.Information DateOfBirth,
                               Models.Information HourOfBirth,
                               Models.Information NumberOfKids,
                               Models.Information Emso,
                               Models.Information Email,
                               Models.Information PlaceOfBirth)
    {
        if (ModelState.IsValid)
        {
            Models.Information info = new Models.Information();

            info.FirstName = FirstName.ToString();
            info.LastName = LastName.ToString();
            info.DateOfBirth = Convert.ToDateTime(DateOfBirth);
            info.HourOfBirth = Convert.ToDateTime(HourOfBirth);
            info.NumberOfKids = Convert.ToInt32(NumberOfKids);
            info.Emso = Emso.ToString();
            info.Email = Email.ToString();
            info.PlaceOfBirth = PlaceOfBirth.ToString();

            TempData["info"] = info;

            return RedirectToAction("Summary");
        }

        return View();
    }

    //Show info
    [HttpPost]
    public ActionResult Post(Models.Information info)
    {
        info.FirstName = ViewData["FirstName"].ToString();
        info.LastName = ViewData["LastName"].ToString();
        info.DateOfBirth = Convert.ToDateTime(ViewData["DateOfBirth"]);
        info.HourOfBirth = Convert.ToDateTime(ViewData["HourOfBirth"]);
        info.NumberOfKids = Convert.ToInt32(ViewData["NumberOfKids"]);
        info.Emso = ViewData["Emso"].ToString();
        info.Email = ViewData["Email"].ToString();
        info.PlaceOfBirth = ViewData["PlaceOfBirth"].ToString();

        return View();
    }
}

我尝试在索引页面上显示数据,如下所示:

 First name: <input type='text' runat="server" value="@ViewData["FirstName"]" /><br />
4

1 回答 1

4

你的代码看起来有点让我困惑,我不确定你想要完成什么。但首先,让我们举一个我认为你正在努力完成的例子。

  1. 用户在信息操作/视图上输入信息
  2. (POST)这被发布到控制器/动作,然后验证模型并放入 ViewData(重定向到第 3 步)
  3. (GET) 您希望在摘要页面上重新显示此数据。

在这些假设下,让我们稍微清理一下您的代码,利用 ViewData 和 TempData 对象的使用,看看我们是否可以完成这项工作。

信息行动

public ActionResult Information()
    {
        //In a strongly typed view, typically you would send an empty model
        //so the model binder/Html.Input helpers have something to bind to
        return View(new Models.Information());
    }

用于处理传入数据的信息 POST

[HttpPost]
public ActionResult Information(Models.Information info)
{
    //Yup - you can use the same action name as the GET action above
    //You can name this anything you want, but I typically keep the same names so 
    //I know where the data came from

    //I think your assignments are backwards.  info came into the controller action
    //You want to save this as ViewData.  Also, let's do that in one line of code
    //And since we are redirecting, lets use the TempData dictionary

    //Notice the method type decorator above [HttpPost].  This is the P in PRG (Post)

if(modelState.IsValid()){
    TempData["info"] = info;

    //Notice the name of the ActionMethod below.  The is the R in PRG (Redirect)
    return RedirectToAction("Summary");
}

//There were errors, lets send back to the Information view
return View(info);

}

总结行动

public ActionResult Summary()
    {
        //We were redirected here, and this is a GET method.  This is the G in PRG
        //(GET)

        //lets go ahead and set the TempData stuff to a model, it just looks nicer on
        //the view
        var model = TempData["info"];
        return View(model);
    }

至于视图,这里是每个看起来像的片段

信息视图

@model Models.Information

@{using(Html.BeginForm()){
   @Html.LabelFor(x=>x.FirstName)<br/>
   @Html.TextBoxFor(x=>x.Firstname)

   //Repeat for each property

   <input type="Submit" value="Submit"/>
}}

摘要视图

@model Models.Information

   @Html.LabelFor(x=>x.FirstName)<br/>
   @Html.DisplayFor(x=>x.Firstname)

   //Repeat for each property
于 2013-04-06T21:32:09.123 回答