ASP.Net MVC4剃刀ViewBag
我使用的是ViewBag
为我检查点partial view
我应该呈现在某个div来这,这里是我的代码:ASP.Net MVC4剃刀ViewBag
在控制器:
[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
工作正常..
我的代码有什么问题?
你为什么不嵌入表单中的步骤(在你的谐音)?
这样,无论何时提交表单,您都会得到它的哪一步,并从控制器代码显示正确的下一步,而不是视图。
您可以将其添加为您的ViewModel的属性,或者只是简单地发布它并从您的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从控制器传递数据进行查看,这是预期的用途。
希望它对你有意义。
HTTP is a stateless protocol。这意味着每个请求都是新的,客户端和服务器之间的任何以前的交互大多是“被遗忘”的。
每次用户发出请求时,ViewBag都会从头开始重新初始化。在我看来,您的代码假设ViewBag在客户端的浏览器中“挂起”,直到他们发出下一个请求。不幸的是事情不这样。
ViewBag.NextForm
等于"Services"
永远不会发生,因为应用程序不知道用户在哪里。
在整个HTTP应用程序中维护状态是一个长期和持续的讨论:How can I keep "state" information between calls to my CGI program?(1996?)。
没有一个明显的解决方案,我可以给你。一种方法是将表单中的信息保存为隐藏输入和POST数据返回到您的控制器,以便它知道用户在哪里。您也可以在cookies或会话中保存信息。
你好罗文先生,使用会话/ cookies是我的最后一招,尽可能的,我真的不想使用会话/ cookies由于机密信息。 –
你为什么不使用AJAX这个
@using (Ajax.BeginForm("Action", "Controller", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "PartialContainer" }))
{
}
的UpdateTargetId =“PartialContainer”将更新与局部视图的DIV,你必须返回来自控制器的局部视图
其实,我是新来的MVC4剃须刀,我真的不知道如何使用Ajax.BeginForm,但我会尝试它。谢谢 –
@JohnPauloCastro,然后检查这个视频,这将帮助你http://www.youtube.com/watch?v=B59skvlroyc – Golda
你好Golda,它不工作。 –
是的,确实HTTP是无状态协议。
您的值将在请求之间丢失。
ASP.Net webforms使用ViewState
来解决此问题。
ASP.Net MVC不支持ViewState。
为什么不把你的数据放在会话?否则,你可以让Ajax调用防止entire page refresh
由于机密数据,我无法使用会话,而且我不善于处理会话,并且对于ajax调用,我尝试了但ViewBag.nextForm的值保持不变。 –
这个方法真的帮了我很多。 –