我有問題將我的模型發送到我的控制器中。 我使用ajax按鈕來改變頁面,但我需要模型誰是第一頁到第二頁。 我想在我的控制器中發送我的模型,但它不起作用。我無法在我的控制器中發送我的模型
當我進入頁面CreateAll時,renderpartial跟着工作來顯示step1,但是如果我點擊step2我想mainModel發送到我的控制器使用子模型的部分視圖。
我的模式是:
public class CreateAllStep
{
public CreateStep1 Step1 { get; set; }
public CreateStep2 Step2 { get; set; }
public CreateAllStep(CreateStep1 step1, CreateStep2 step2)
{
this.Step1 = step1;
this.Step2 = step2;
}
}
我的控制器(當頁面開始):
public ActionResult Create()
{
CreateStep1 step1=FillStep1();
CreateStep2 step2 = FillStep2();
CreateAllStep allStep = new CreateAllStep(step1, step2);
return View(allStep);
}
我的控制器(當我點擊按鈕,它在這兒,我想送模型):
[HttpPost]
public ActionResult Create(String btn, CreateAllStep form)
{
if (Request.IsAjaxRequest())
{
if (btn != null)
{
if (btn == "Step1")
{
return PartialView("Step1",form.Step1);//not work
}
else if (btn == "Step2")
{
return PartialView("Step2");//work
}
else if(btn =="AllStep")
{
return PartialView("AllStep");
}
}
}
return View();
}
而我的主要觀點是:
@model SiteWebEmpty.Models.CreateAllStep
@{
ViewBag.Title = "Title";
}
<script type="text/javascript">
$(function() {
$('form').submit(function() {
$.post(this.action, $(this).serialize(), function (data) {
alert(data);
});
return false;
});
});
</script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<h2>Title</h2>
@using (Ajax.BeginForm("Create", //controller action name
"CreateStep", //controller name
new AjaxOptions //ajax options that tell mvc how to perform the replacement
{
UpdateTargetId = "ViewPage", //id of div to update
HttpMethod = "Post" //how to call the controller action
}, new { id = "FormName" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Create </legend>
<button type="submit" name="btn" value="Step1" id="Step1">Step 1</button>
<button type="submit" name="btn" value="Step2" id="Step2">Step 2</button>
<button type="submit" name="btn" value="AllStep" id="AllStep">All Step</button>
<div id="ViewPage">
@Html.Partial("Step1", Model)
</div>
</fieldset>
}
我的部分觀點是:
@model SiteWebEmpty.Models.ArticleRequest.CreateArticle.ArticleRequestDisplayCreateAllStep
<fieldset>
<legend>Step 1</legend>
@Html.LabelFor(step1 => step1.Step1.Customer)
@Html.EditorFor(step1 => step1.Step1.Customer)
@Html.ValidationMessageFor(step1 => step1.Step1.Customer)
@Html.LabelFor(articleType => articleType.Step1.ArticleType)
@Html.DropDownList("ArticleType", Model.Step1.ArticleType)
@Html.ValidationMessageFor(articleType => articleType.Step1.ArticleType)
@Html.LabelFor(model => model.Step1.LabelType)
@Html.DropDownList("LabelType", Model.Step1.LabelType)
@Html.ValidationMessageFor(model => model.Step1.LabelType)
</fieldset>
渲染HTML:
<script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
<h2>Title</h2>
<form action="/CreateStep/Create?Length=13" data-ajax="true" data-ajax-method="Post" data-ajax-mode="replace" data-ajax-update="#ViewPage" id="FormName" method="post"> <fieldset>
<legend>Create </legend>
<button type="submit" name="btn" value="Step1" id="Step1">Step 1</button>
<button type="submit" name="btn" value="Step2" id="Step2">Step 2</button>
<button type="submit" name="btn" value="AllStep" id="AllStep">All Step</button>
<div id="ViewPage">
<fieldset>
<legend>Step 1</legend>
<label for="Customer">Customer</label>
<input class="text-box single-line" data-val="true" data-val-required="Customer is required" id="Customer" name="Customer" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="Customer" data-valmsg-replace="true"></span>
<label for="ArticleType">Article Type</label>
<select data-val="true" data-val-required="ArticleType is required" id="ArticleType" name="ArticleType"><option value="127">AR1 : New Product</option>
<option value="161">AR2 : Product Modification</option>
</select>
<span class="field-validation-valid" data-valmsg-for="ArticleType" data-valmsg-replace="true"></span>
<label for="LabelType">Label Type</label>
<select data-val="true" data-val-required="LabelType is required" id="LabelType" name="LabelType"><option value="129">Private Label</option>
</select>
<span class="field-validation-valid" data-valmsg-for="LabelType" data-valmsg-replace="true"></span>
</fieldset>
</div>
</fieldset>
</form>
感謝您的幫助:)!
@Martin如果我刪除了我的所有構造函數,那不是工作:s – Zoners 2012-03-20 13:08:36
@Fujiy我更新了我的帖子,更多的說明:)如果你需要某些東西來理解我的問題,打電話給我:)而且我不明白當你說「 @ Html.Partial(「Step1」,Model.Step1)將呈現一個帶有Customer或ArticleType等id的輸入文本,而不是Step1.Customer和Step1.ArticleType。 – Zoners 2012-03-20 13:09:34
我更新我的答案來修復你的bug – Martin 2012-03-20 13:12:34