[英]I can't send my model in my controller
I have a problem to send my Model into my controller. 我将模型发送到控制器时遇到问题。 I use a button with the ajax to change the page but i need the model who is the first page to the second page.
我使用带有ajax的按钮来更改页面,但是我需要第一页到第二页的模型。 I would like send my model in my controller but it's not work.
我想在控制器中发送模型,但无法正常工作。
When i come in the page CreateAll, the renderpartial follow works to display Step1 but if i click on the step2 i would like the mainModel to be send to my controller use a partial view with submodel. 当我进入页面CreateAll时,renderpartial Follow可以显示Step1,但是如果我单击step2,我希望将mainModel发送到我的控制器,请使用带有子模型的局部视图。
My model is: 我的模型是:
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;
}
}
My controller is(when the page start): 我的控制器是(页面开始时):
public ActionResult Create()
{
CreateStep1 step1=FillStep1();
CreateStep2 step2 = FillStep2();
CreateAllStep allStep = new CreateAllStep(step1, step2);
return View(allStep);
}
My controller is(when i click on the button, it's here where i would like send the model): 我的控制器是(当我单击按钮时,它是我想发送模型的位置):
[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();
}
And my main view is : 我的主要观点是:
@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>
}
My partial view is: 我的部分观点是:
@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>
render html: 渲染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>
Thanks for your help :) ! 谢谢你的帮助 :) !
Could you post the final HTML? 您可以发布最终的HTML吗?
I think that your @Html.Partial("Step1", Model.Step1) will render a input text with id like Customer or ArticleType instead of Step1.Customer and Step1.ArticleType. 我认为您的@ Html.Partial(“ Step1”,Model.Step1)将呈现ID为Customer或ArticleType的输入文本,而不是Step1.Customer和Step1.ArticleType。 What will bind to CreateAllStep.Customer that doesn´t exist.
绑定到不存在的CreateAllStep.Customer的对象。
If you have the Headers sent by the browsers will help too. 如果您有浏览器发送的标题,也会有所帮助。
Update: Change your partial Step1, to accept a CreateAllStep Model and try again 更新:更改您的部分Step1,以接受CreateAllStep模型,然后重试
Try removing your constructor on your ViewModel: 尝试在ViewModel上删除构造函数:
public CreateAllStep(CreateStep1 step1, CreateStep2 step2)
{
this.Step1 = step1;
this.Step2 = step2;
}
And change the controller code to: 并将控制器代码更改为:
public ActionResult Create()
{
CreateAllStep allStep = new CreateAllStep{Step1 = FillStep1(), Step2 = FillStep2()};
return View(allStep);
}
I have run into issues with constructors with parameters when databinding. 数据绑定时,我遇到了带有参数的构造函数的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.