I am trying my best to use ViewModels correctly in my web application, but I am having various problems. One of them, if I set a breakpoint immediately after publishing using the Create action, my viewModel did not save any of my form values. I have to do something wrong, but I have tried several things. Including the code below, where I call the form elements the same as the viewModel fields to see if this helps.
I am also interested in which particular properties in your viewmodel should represent. I have seen people use different things in blog posts and much more.
If the view will display a selection list, I get the impression that the viewmodel should have an IEnumerable SelectListItem for this, as shown below. However, I have seen people use the IEnumerable Entity to represent a type that represents a select list.
Can anyone shed some light on this for me? Yesterday I gave up all my business logic to start a new job and try and do it right.
My ViewModel:
public class ServerCreateViewModel
{
public int Id { get; set; }
public IEnumerable<SelectListItem> CompanyName { get; set; }
public IEnumerable<SelectListItem> GameTitle { get; set; }
public IEnumerable<SelectListItem> City { get; set; }
public IEnumerable<SelectListItem> NumberOfPlayers { get; set; }
public IEnumerable<SelectListItem> CurrencyAbbreviation { get; set; }
}
My Controller action:
public ActionResult Create()
{
var viewModel = new ServerCreateViewModel();
viewModel.CompanyName = new SelectList(_dataService.Companies.All(), "Id", "CompanyName");
viewModel.GameTitle = new SelectList(_dataService.Games.All(), "Id", "GameTitle");
viewModel.City = new SelectList(_dataService.Locations.All(), "Id", "City");
viewModel.NumberOfPlayers = new SelectList(_dataService.ServerPlayers.All(), "Id", "NumberOfPlayers");
return View(viewModel);
}
[HttpPost]
public ActionResult Create(FormCollection collection, ServerCreateViewModel viewModel)
{
try
{
return Content("Success");
}
catch
{
return Content("Fail");
}
}
My view:
@model GameserverCompare.ViewModels.Server.ServerCreateViewModel
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Server</legend>
@Html.HiddenFor(m => m.Id)
<div class="editor-label">
@Html.LabelFor(model => model.CompanyName)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => Model.CompanyName, Model.CompanyName)
@Html.ValidationMessageFor(model => model.CompanyName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.GameTitle)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => Model.GameTitle, Model.GameTitle)
@Html.ValidationMessageFor(model => model.GameTitle)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.City)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => Model.City, Model.City)
@Html.ValidationMessageFor(model => model.City)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NumberOfPlayers)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => Model.NumberOfPlayers, Model.NumberOfPlayers)
@Html.ValidationMessageFor(model => model.NumberOfPlayers)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}