ASP.Net MVC4 Razor ViewBag

时间:2014-03-27 03:57:11

标签: asp.net-mvc asp.net-mvc-4 razor

我使用ViewBag作为我的检查点,我应该在某个div上呈现partial view,这是我的代码:

在控制器中:

    [HttpPost]
    public ActionResult NewAppointment(appointment.AppointmentInformation model)
    {
        if (ViewBag.NextForm == null || ViewBag.NextForm == "undefined")
        {
            info.CustomerType = model.CustomerType;
            ViewBag.NextForm = "Information";
        }
        else if (ViewBag.NextForm == "Information")
        {
            info.CustomerID = String.IsNullOrEmpty(model.CustomerID) ? "" : model.CustomerID;
            info.CustomerName = String.IsNullOrEmpty(model.CustomerName) ? "" : model.CustomerName;
            info.CustomerCNum = String.IsNullOrEmpty(model.CustomerCNum) ? "" : model.CustomerCNum;
            ViewBag.NextForm = "Services";
        }
        else if (ViewBag.NextForm == "Services")
        {
            //do nothing;
        }
        return View(info);
    }

在视图中

<form method="post">
<div id="PartialContainer">

    @if (ViewBag.NextForm == null || ViewBag.NextForm == "undefined")
    {
        @Html.Partial("CustomerType")
    }
    @if (ViewBag.NextForm == "Information")
    {
        @Html.Partial("GuestInformation")
    }
    @if (ViewBag.NextForm == "Services")
    {
        @Html.Partial("Service")
    }

</div>
<div id="ButtonArea">
    <button id="btnCancel">Cancel</button>
    <button id="btnBack">Back</button>
    <button id="btnNext">Next</button>
</div>

第三次点击btnNext时,@ Html.Part @Html.Partial("Service")无效。但前两个@Html.Partial工作正常..

我的代码出了什么问题?

4 个答案:

答案 0 :(得分:1)

为什么不在表单中嵌入步骤(在你的部分中)?

这样,无论何时提交表单,您都会获得它的步骤,并从您的控制器代码而不是视图中显示正确的下一步。

您可以将其添加为ViewModel的属性,或者只是将其POST并从Controller中的Request对象获取。

的伪代码:

[HttpPost]
    public ActionResult NewAppointment(appointment.AppointmentInformation model)
    {
        //get the current step or start with empty string
        //although if this is the POST method, you should have the
        //first value, set in your GET method to show the form!
        var step = Request.Params["NextForm"] ?? "";

        if (step == "")
        {
            info.CustomerType = model.CustomerType;
            ViewBag.NextForm = "Information";
        }
        else if (step == "Information")
        {
            info.CustomerID = String.IsNullOrEmpty(model.CustomerID) ? "" : model.CustomerID;
            info.CustomerName = String.IsNullOrEmpty(model.CustomerName) ? "" : model.CustomerName;
            info.CustomerCNum = String.IsNullOrEmpty(model.CustomerCNum) ? "" : model.CustomerCNum;
            ViewBag.NextForm = "Services";
        }
        else if (step == "Services")
        {
            //do nothing;
        }
        return View(info);
    }

然后在您的视图中使用该值,随时随表单一起发送。

<form method="post">
<div id="PartialContainer">
    <input type="hidden" name="NextForm" value="@(ViewBag.NextForm ?? "")" />

    @if (ViewBag.NextForm == null || ViewBag.NextForm == "undefined")
    {
        @Html.Partial("CustomerType")
    }
    @if (ViewBag.NextForm == "Information")
    {
        @Html.Partial("GuestInformation")
    }
    @if (ViewBag.NextForm == "Services")
    {
        @Html.Partial("Service")
    }

</div>
<div id="ButtonArea">
    <button id="btnCancel">Cancel</button>
    <button id="btnBack">Back</button>
    <button id="btnNext">Next</button>
</div>

您每次都会使用表单获取NextStep,然后您只需使用ViewBag将数据从控制器传递到视图,这是预期用途。

希望你有意义。

答案 1 :(得分:0)

HTTP is a stateless protocol。这意味着每个请求都是,并且客户端和服务器之间之前的任何交互都被“忘记了”#34;。

每次用户发出请求时,都会从头开始重新初始化ViewBag。在我看来,你的代码假定ViewBag&#34;挂在&#34;在客户端的浏览器中,直到他们发出下一个请求。不幸的是,事情不会以这种方式发挥作用。

ViewBag.NextForm等于"Services"将永远不会发生,因为应用程序不知道用户所在的位置。

在整个HTTP应用程序中维护状态是一个漫长而持续的讨论:How can I keep "state" information between calls to my CGI program?(1996?)。

我可以给你一个明显的解决方案。一种方法是将表单中的信息保存为隐藏输入,并将POST数据保存回控制器,以便它知道用户所在的位置。您还可以在 Cookie 会话中保存信息。

答案 2 :(得分:0)

为什么不为此使用ajax

@using (Ajax.BeginForm("Action", "Controller", new AjaxOptions { HttpMethod = "POST",  UpdateTargetId = "PartialContainer" }))
{
}

UpdateTargetId =“PartialContainer”将使用部分视图更新您的div,您必须从控制器返回局部视图

答案 3 :(得分:0)

是的,确实 HTTP 无状态协议

您的价值观将在请求之间丢失。

ASP.Net webforms 使用ViewState来解决此问题。

ASP.Net MVC 不支持 ViewState。

为什么不将数据放在会话中?或者,您可以进行阻止entire page refresh

ajax调用