I have a problem to send my List<SelectList> 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.
I would like send my model with my List<SelectList> in my controller, it's work if is a simple data but not to dropdownlist with List<SelectListitem>, i would like keep List<SelectListItem> with my object and no keep a simple string when the people click in
the dropdownList.
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 keep my object with into the List<SelectList>):
[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 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)//the problem is here, this send a string and not the all List<SelectListItem>
@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>
the broswer does not send the model to the controller (unless you are using javscript and sending json models built in the browser). the browser just does a form post, sending name/value pairs for the form fields (input, textarea, select). mvc when it call
the controller action creates a new model and binds the postback values to it.
a dropdown (select) only sends back the selected value. so if you want the whole selectlist posted back, you will need to serialize it to a hidden field.
mvcasp.netaspasp.netmvc
bruce (sqlwork.com)
Marked as answer by ricka6 on Mar 30, 2012 04:49 PM
Zoners
0 Points
3 Posts
Is it possible send a List<SelectListemItem> with a dropdownList?
Mar 21, 2012 04:07 PM|LINK
I have a problem to send my List<SelectList> 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.
I would like send my model with my List<SelectList> in my controller, it's work if is a simple data but not to dropdownlist with List<SelectListitem>, i would like keep List<SelectListItem> with my object and no keep a simple string when the people click in the dropdownList.
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 keep my object with into the List<SelectList>):
[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 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)//the problem is here, this send a string and not the all List<SelectListItem> @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>Thanks for your help :) !
mvc asp.net asp asp.netmvc
mm10
Contributor
6409 Points
1184 Posts
Re: Is it possible send a List<SelectListemItem> with a dropdownList?
Mar 30, 2012 08:53 AM|LINK
Use Html.DropDownListFor In your view:
@Html.DropDownListFor(m=>m.ArticleType, Model.Step1.ArticleType)
mvc asp.net asp asp.netmvc
bruce (sqlwo...
All-Star
36850 Points
5445 Posts
Re: Is it possible send a List<SelectListemItem> with a dropdownList?
Mar 30, 2012 01:32 PM|LINK
the broswer does not send the model to the controller (unless you are using javscript and sending json models built in the browser). the browser just does a form post, sending name/value pairs for the form fields (input, textarea, select). mvc when it call the controller action creates a new model and binds the postback values to it.
a dropdown (select) only sends back the selected value. so if you want the whole selectlist posted back, you will need to serialize it to a hidden field.
mvc asp.net asp asp.netmvc