I’m new in asp.net mvc3 programming and I’m trying to build a specific form. I need to have a form with the user field (which I have) but also a list of object (in that case SStatus).
My form :
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Création d'utilisateur</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Lastname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Lastname)
@Html.ValidationMessageFor(model => model.Lastname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Firstname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Firstname)
@Html.ValidationMessageFor(model => model.Firstname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Login)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Login)
@Html.ValidationMessageFor(model => model.Login)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<p>Status</p>
@{
//The error was form here
@{
var list = ViewBag.listStatus as List<SStatus>;
}
@if (list != null)
{
foreach(var status in list)
{
<option value=@status.ID>@status.Name</option>
}
}
</select>
}
<p>
<input type="submit" value="Création" />
</p>
</fieldset>
}
The list call :
public ActionResult CreateUserView()
{
RestClient client = new RestClient(Resource.Resource.LocalUrlService);
RestRequest request = new RestRequest("/status/all", Method.GET);
var response = client.Execute(request);
if(response.StatusCode == HttpStatusCode.OK)
{
List<SStatus> listSatus = JsonHelper.FromJson<List<SStatus>>(response.Content);
ViewBag.listStatus = listSatus;
}
return View();
}
And the form post:
[HttpPost]
public ActionResult CreateUserView(Uuser userToCreate, string list)
{
//list got the ID of SStatus.
if (ModelState.IsValid)
{//Stuff}
}
So the question is : How get the selected list item ?
Regards.
Use a view model pattern. I still don’t see how your
Uuserobject is sent to the view (via the default[HttpGet]action, but I think I see what you’re trying to accomplish.) If you refactor this way, you’ll still get to use the built-in validation, automagic model binding, etc.Then your action parameter should be of type
CreateUserViewModele.g.I believe you’ll need a
nameattribute on the<select>element in order for it to be posted.Although, you’re going to run into trouble if the model isn’t valid. Your view should be strongly typed against
CreateUserViewModele.g.So, your
Lastnameproperty might look like this (note the.User)And finally, I guess you could keep the possible list of status in the ViewBag, but you’ll want to set the selected value to
@Model.Status. You may want to consider changingCreateUserViewModel.Statusto aList<SelectListItem>that you can populate from your controller e.g. yourGETaction shouldreturn View(CreateUserViewModel)