I have my model for my users in MVC3:
public class User
{
[Key]
public virtual Guid UserId { get; set; }
[Required]
public virtual String Username { get; set; }
[DataType(DataType.MultilineText)]
public virtual String Comment { get; set; }
..loads of other properties
This class contains loads of info that I don't really need in an edit screen. Let's say I only wanted to edit the comment
part of the user
. I tried to create a viewmodel like this but not sure if im going about this right
view model
public class UserViewModel
{
public string notes;
}
Now I have this in my controller:
public ActionResult Edit(Guid id)
{
User user = Context.Users.Where(x => x.UserId == id).First();
UserViewModel uvm = new UserViewModel();
uvm.notes = user.Comment;
return View(uvm);
}
This part seems to work ok and passes the info through if it exists. The edit view is like this:
@model core.Areas.Manage.Models.UserViewModel
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>User Edit</legend>
@Html.TextBoxFor(model => model.notes)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
Which shows the notes/comment from the controller.
However, if i press submit, the model is nulled - The model.notes
is null at this point:
[HttpPost]
public ActionResult Edit(Guid id, UserViewModel model)
{
Am i doing something completely wrong? In the past I simply passed the whole model around, but this isn't really ideal. Im kind of taking a stab in the dark here, just thought i'd try to implement it myself before turning to SO!