因此,我正在開發基本上是三個步驟的MVC。MVC通過多視圖提交維護模型
創建的每一步即 的StepOne StepTwo StepThree
在工序一個和兩個我要求用戶輸入一些細節的視圖。
多步驟I的所有值都存儲在一個模型中。
從StepOne到StepTwo是很好的。我的模型中的某些值正在設置和維護。
但在StepTwo當我做我的第二個httppost並通過模型,它似乎只是創建模型,並從的StepOne值的新實例不會保留。
<% using (Html.BeginForm("StepTwo", "Home", FormMethod.Post, new { id = "restrictionForm" })) { %>
<%: Html.AntiForgeryToken() %>
<div id="wrapping" class="clearfix">
<h3>Postcode Restriction Type : </h3>
<%= Html.DropDownListFor(x => x.SelectedRestriction, Model.RestrictionTypes,"Select Restriction...", new { @class = "selmenu required" }) %>
<h3>Restriction Description : </h3>
<%= Html.TextBoxFor(m => m.RestrictionDescription, new { @class = "txtblock required" }) %>
</div>
<section id="buttons">
<input type="submit" value="Submit" id="submitBtn" />
</section>
而且在我的控制器
在頁面加載我的模式依然不變,仍從前面的步驟維護值。
[Authorize]
public ActionResult StepTwo(PostcodesModel model)
{
var summaryMessage = "";
model.SummaryMessage = summaryMessage;
model.RestrictionTypes = _Provider.GetRestrictionTypes();
return View(model);
}
但是在Httppost,模型已經失去了價值,似乎已經創建了模型的新實例。
[Authorize]
[HttpPost]
[ActionName("StepTwo")]
[ValidateAntiForgeryToken]
public ActionResult StepTwoPost(PostcodesModel model)
{
return View(model);
}
任何想法如何維護Http Posts之間的模型?
謝謝,發現有用的東西。在我的第二篇Http Post中,我可以添加現有的模型值,然後將其保留。<%using(Html.BeginForm(「StepTwo」,「Home」,Model,FormMethod.Post,new {id =「restrictionForm」})){%> – StevieB
好東西。如果您發現某個值在發回時未保留,則可能需要將其添加爲隱藏字段。例如,如果您輸出某人的姓名並在您的視圖中調用@ Model.Name,那麼除非您添加隱藏的輸入@ Html.HiddenFor(m => m.Name ) –
好吧,是的,我剛剛注意到,當我將模型添加到我的表單聲明時,我的模型的所有值都顯示在URL中,如查詢字符串對象,這不是很好 – StevieB