[英]How can i send back a model containing two lists from the view to my controller
[英]I can't send my model in my controller
我將模型發送到控制器時遇到問題。 我使用帶有ajax的按鈕來更改頁面,但是我需要第一頁到第二頁的模型。 我想在控制器中發送模型,但無法正常工作。
當我進入頁面CreateAll時,renderpartial Follow可以顯示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>
謝謝你的幫助 :) !
您可以發布最終的HTML嗎?
我認為您的@ Html.Partial(“ Step1”,Model.Step1)將呈現ID為Customer或ArticleType的輸入文本,而不是Step1.Customer和Step1.ArticleType。 綁定到不存在的CreateAllStep.Customer的對象。
如果您有瀏覽器發送的標題,也會有所幫助。
更新:更改您的部分Step1,以接受CreateAllStep模型,然后重試
嘗試在ViewModel上刪除構造函數:
public CreateAllStep(CreateStep1 step1, CreateStep2 step2)
{
this.Step1 = step1;
this.Step2 = step2;
}
並將控制器代碼更改為:
public ActionResult Create()
{
CreateAllStep allStep = new CreateAllStep{Step1 = FillStep1(), Step2 = FillStep2()};
return View(allStep);
}
數據綁定時,我遇到了帶有參數的構造函數的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.