I have a model and an enumerable of that model that I am putting together into a view model in asp.net mvc3 like so.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespace FuelPortal.Models.DBModels
{
public class UserDBModel
{
public int userId { get; set; }
[Required, Display(Name = "User Name")]
public string userName { get; set; }
[Required, Display(Name = "Password")]
public string password { get; set; }
[Required, Display(Name = "First Name")]
public string firstName { get; set; }
[Required, Display(Name = "Last Name")]
public string lastName { get; set; }
[Required, Display(Name = "Email")]
public string email { get; set; }
[Display(Name = "Phone")]
public string phone { get; set; }
[Display(Name = "Account Expiration Date(Optional)")]
public DateTime expireDate { get; set; }
[Display(Name = "Password Expiration Date")]
public DateTime passwordExpireDate { get; set; }
[Display(Name = "Last Date Modified")]
public DateTime DateModified { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FuelPortal.Models.DBModels;
namespace FuelPortal.Models.ViewModels
{
public class UserMaintenanceViewModel
{
public UserDBModel user = new UserDBModel();
public List<UserDBModel> userList = new List<UserDBModel>();
}
}
Now I strongly type a view to use the view model.
#model FuelPortal.Models.ViewModels.UserMaintenanceViewModel
<div class="frmBG">
#using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "wrapperData" }))
{
<h5>Create New User</h5>
<br />
<table cellpadding="2" cellspacing="10" border="0">
<tr><td>
<div class="editor-label">
#Html.LabelFor(model => model.user.userName)
</div></td><td>
<div class="editor-field">
#Html.EditorFor(model => model.user.userName)
#Html.ValidationMessageFor(model => model.user.userName)
</div></td></tr>
<tr><td>
<div class="editor-label">
#Html.LabelFor(model => model.user.password)
</div></td><td>
<div class="editor-field">
#Html.PasswordFor(model => model.user.password)
#Html.ValidationMessageFor(model => model.user.password)
</div></td></tr>
<tr><td>
<div class="editor-label">
#Html.LabelFor(model => model.user.firstName)
</div></td><td>
<div class="editor-field">
#Html.EditorFor(model => model.user.firstName)
#Html.ValidationMessageFor(model => model.user.firstName)
</div></td></tr>
<tr><td>
<div class="editor-label">
#Html.LabelFor(model => model.user.lastName)
</div></td><td>
<div class="editor-field">
#Html.EditorFor(model => model.user.lastName)
#Html.ValidationMessageFor(model => model.user.lastName)
</div></td></tr>
<tr><td>
<div class="editor-label">
#Html.LabelFor(model => model.user.email)
</div></td><td>
<div class="editor-field">
#Html.EditorFor(model => model.user.email)
#Html.ValidationMessageFor(model => model.user.email)
</div></td></tr>
<tr><td>
<div class="editor-label">
#Html.LabelFor(model => model.user.phone)
</div></td><td>
<div class="editor-field">
#Html.EditorFor(model => model.user.phone)
#Html.ValidationMessageFor(model => model.user.phone)
</div></td></tr>
<tr><td>
<div class="editor-label">
#Html.LabelFor(model => model.user.expireDate)
</div></td><td>
<div class="editor-field">
#Html.EditorFor(model => model.user.expireDate)
#Html.ValidationMessageFor(model => model.user.expireDate)
</div></td></tr>
<tr><td></td><td>
<p>
<input type="submit" value="" class="create" /><input type="button" value="" class="close_tab"/>
</p></td></tr>
</table>
}
</div>
For some reason on post it does not populate the object within the view model. So when i create a post action all values of (UserMaintenanceViewModel model) are null. Like so..
[HttpPost]
public ActionResult CreateUser(UserMaintenanceViewModel model)
{
try
{
/// TODO: Add insert logic here
var result = repository.insertNewUser(Session["UserId"].ToString(), model.user.UserName, model.user.Password, model.user.FirstName,
model.user.LastName, model.user.Email, model.user.Phone, model.user.ExpireDate);
// If result > 0 user insert successful
Also, if I switch to getting postback through a (FormCollection form) I see that the names of the html elements are "user.UserName" instead of the expected "UserName". Any guidance would be greatly appreciated I feel like I should be able to accomplish this.
Thanks so much
If you have a complex view model which is what you have, then you have to make sure to create instance of an object in the constructor. You can then add info to any child object since it now has an instance.
public class MyViewModel
{
public MyViewModel()
{
Customer= new CustomerViewModel ();
Address = new AddressViewModel();
}
public int OrderNumber{ get; set; }
public CustomerViewModel Customer { get; set; }
public AddressViewModel Address { get; set; }
}
}
Related
I want to populate a dropdownlist on create view with form also. Here is my code
#model CM.Models.TVSerialEpisode
#{
ViewBag.Title = "AddEpisode";
}
#Html.Action("ListAllSeason","TvSerial") //here i want to call this action method so i could bind all tv serial
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>TVSerialEpisode</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Serial_ID)
</div>
<div class="editor-field">
#Html.DropDownList("Serial_ID", new List<SelectListItem>
{
new SelectListItem { Text = "Backstrom", Value="16" },
new SelectListItem{Text="10 Things I hate about You", Value="15"},
new SelectListItem { Text = "Castle", Value="14" }
}, "Select Serial")
#Html.ValidationMessageFor(model => model.Serial_ID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Episode_No)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Episode_No)
#Html.ValidationMessageFor(model => model.Episode_No)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Episode_Name)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Episode_Name)
#Html.ValidationMessageFor(model => model.Episode_Name)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.File_Url_480p)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.File_Url_480p)
#Html.ValidationMessageFor(model => model.File_Url_480p)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.File_Url_720p)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.File_Url_720p)
#Html.ValidationMessageFor(model => model.File_Url_720p)
</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>
<div class="editor-label">
#Html.LabelFor(model => model.Is_Active)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Is_Active)
#Html.ValidationMessageFor(model => model.Is_Active)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
here is the model class
namespace CM.Models
{
[Table("tbltvserial")]
public class TVSerial
{
[Key]
public Int64 Serial_ID { get; set; }
public string Serial_Name { get; set; }
public string Season_Name { get; set; }
public int Season_No { get; set; }
public int? Release_Year { get; set; }
}
public class TVSerial_VM
{
public Int64 Serial_ID { get; set; }
public string Serial_Name { get; set; }
public int Season_No { get; set; }
}
[Table("tblserialepisode")]
public class TVSerialEpisode
{
[Key]
public Int64 Video_ID { get; set; }
public Int64 Serial_ID { get; set; }
public int Episode_No { get; set; }
public string Episode_Name { get; set; }
public string File_Url_480p { get; set; }
public string File_Url_720p { get; set; }
public string Description { get; set; }
public bool Is_Active { get; set; }
public DateTime Uploaded_Time { get; set; }
}
public class TvSerialContext : DbContext
{
public DbSet<TVSerialEpisode> TvSerialEpisodes { get; set; }
public DbSet<TVSerial> TvSerials { get; set; }
}
}
here are controller class
public class TvSerialController : Controller
{
public List<TVSerial_VM> ListAllSeason()
{
try
{
TvSerialContext tvContext = new TvSerialContext();
List<TVSerial_VM> tv = tvContext.TvSerials
.Select(t => new TVSerial_VM
{
Serial_Name = t.Serial_Name,
Season_No=t.Season_No,
Serial_ID=t.Serial_ID
}).OrderBy(t=> t.Season_Name).
ToList();
return (tv);
}
catch (Exception ex)
{
return null;
}
}
}
public class TvSerialEpisodeController : Controller
{
TvSerialContext tvContext = new TvSerialContext();
public ActionResult AddEpisode()
{
return View();
}
[HttpPost]
public ActionResult AddEpisode(TVSerialEpisode tvEpisode)
{
if (ModelState.IsValid)
{
tvContext.TvSerialEpisodes.Add(tvEpisode);
tvContext.SaveChanges();
return RedirectToAction("AddEpisode");
}
return View(tvEpisode);
}
}
I don't know how to bind the tvserial list inside above view page. basically i want to choose the serial id from dropdown list, is this posible to call a action method inside the create view.
To populate your SelectList from the database, in the controllers AddEpisode() method use
ViewBag.SerialList = db.TvSerials.ToList().Select(x => new SelectListItem(){ Value = x.Serial_ID.ToString(), Text = x.Serial_Name });
and then in the view use
#Html.DropDownListFor(m => m.Serial_ID, (IEnumerable<SelectListItem>)ViewBag.SerialList, "Select Serial")
although it is recommended you use a view model and include a IEnumerable<SelectListItem> SerialList property in the view model so that it is
#Html.DropDownListFor(m => m.Serial_ID, Model.SerialList, "Select Serial")
I would like to save an image/file into a sql-database in an asp.net mvc3 project. I found some examples where they have to save the image into a database with nothing else. When I want to add a "Inzending" instance in my database with the "Create" methode from my Controller, I also need to put the file into an byte array. I have to insert all the information into my database at the same time, because no field may be null. I hope someone can help me with this, I'm just starting with it in school and this is the first real big problem i have to solve ( i find it quite hard to solve). some of my code I already wrote:
Controller:
[HttpPost]
public ActionResult Create(INZENDING inzending)
{
string user = User.Identity.Name;
var users = db.aspnet_Users.Single(p => p.UserName == user).UserId;
inzending.Userid = users;
int id = db.INZENDINGs.Count() + 1;
inzending.InzendingNr = id;
if (ModelState.IsValid)
{
db.INZENDINGs.Add(inzending);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ErfgoedNr = new SelectList(db.ERFGOEDFICHEs, "ErfgoedNr", "Naam", inzending.ErfgoedNr);
ViewBag.Aard = new SelectList(db.INPUT_AARD, "Aard", "Aard", inzending.Aard);
ViewBag.Type = new SelectList(db.INPUTTYPEs, "type", "type", inzending.Type);
ViewBag.Status = new SelectList(db.STATUS, "Waarde", "Waarde", inzending.Status);
return View(inzending);
}
view :
#model Erfgoed.Models.INZENDING
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>INZENDING</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Titel)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Titel)
#Html.ValidationMessageFor(model => model.Titel)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Auteur)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Auteur)
#Html.ValidationMessageFor(model => model.Auteur)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Datum)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Datum)
#Html.ValidationMessageFor(model => model.Datum)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.bestandsgrootte)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.bestandsgrootte)
#Html.ValidationMessageFor(model => model.bestandsgrootte)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Type, "INPUTTYPE")
</div>
<div class="editor-field">
#Html.DropDownList("Type", String.Empty)
#Html.ValidationMessageFor(model => model.Type)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Aard, "INPUT_AARD")
</div>
<div class="editor-field">
#Html.DropDownList("Aard", String.Empty)
#Html.ValidationMessageFor(model => model.Aard)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ErfgoedNr, "ERFGOEDFICHE")
</div>
<div class="editor-field">
#Html.DropDownList("ErfgoedNr", String.Empty)
#Html.ValidationMessageFor(model => model.ErfgoedNr)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.bestand, "Bestand");
</div>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
Model:
namespace Erfgoed.Models
{
using System;
using System.Collections.Generic;
public partial class INZENDING
{
public int InzendingNr { get; set; }
public string Titel { get; set; }
public string Auteur { get; set; }
public System.DateTime Datum { get; set; }
public string bestandsgrootte { get; set; }
public string Type { get; set; }
public string Aard { get; set; }
public byte[] bestand { get; set; }
public System.Guid Userid { get; set; }
public int ErfgoedNr { get; set; }
public string Status { get; set; }
public virtual aspnet_Membership aspnet_Membership { get; set; }
public virtual ERFGOEDFICHE ERFGOEDFICHE { get; set; }
public virtual INPUT_AARD INPUT_AARD { get; set; }
public virtual INPUTTYPE INPUTTYPE { get; set; }
public virtual STATUS STATUS1 { get; set; }
}
}
[HttpPost]
public ActionResult Create(HttpPostedFileBase file, FormCollection collection)
{
var attachment = new Attachment();
if(TryUpdateModel(attachment))
{
long length =
file.InputStream.Length;
attachment.Data = new byte[length];
file.InputStream.Read(attachment.Data, 0, (int)length);
attachmentRepository.Add(attachment);
attachmentRepository.Save();
return RedirectToAction("Index");
}
return null;
}
Here's how i'm saving file in ms sql database. Sorry if i wrongly understood your question
I am using nhibernate and mvc3 in asp.net
I'm trying to add data into table where my table schema is like this:
public class HobbyMasters
{
[Key]
public virtual int HobbyId { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "NameRequired")]
public virtual string HobbyName { get; set; }
public virtual HobbyTypes HobbyType { get; set; }
[Required]
public virtual string HobbyDetails { get; set; }
[Required]
public virtual ICollection<HobbyMasters> HobbyNames { get; set; }
}
public class HobbyTypes
{
[Key]
public virtual int HobbyTypeId { get; set; }
[Required]
public virtual string HobbyType { get; set; }
public virtual ICollection<HobbyTypes> Hobby { get; set; }
}
in my Controller
public ActionResult Create()
{
ViewBag.c1 = (ICollection<HobbyTypes>)(new Hobby_MasterService().GetAllHobbyTypes());
return View();
}
//
// POST: /Hobbies/Create
[HttpPost]
public ActionResult Create(HobbyMasters hobby)
{
ViewBag.c1 = (ICollection<HobbyTypes>)new Hobby_MasterService().GetAllHobbyTypes();
try
{
if (ModelState.IsValid)
{
new Hobby_MasterService().SaveOrUpdateHobby(hobby);
return RedirectToAction("Index");
}
}
}
in the view:
#using (Html.BeginForm("Create", "Hobbies", FormMethod.Post))
{
#Html.ValidationSummary(true)
<fieldset>
<legend>Hobby Master</legend>
<div class="editor-label">
#Html.LabelFor(model => model.HobbyName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.HobbyName)
#Html.ValidationMessageFor(model => model.HobbyName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.HobbyType)
</div>
<div class="Editor-field">
#Html.DropDownListFor(model =>model.HobbyType.HobbyTypeId, new SelectList(ViewBag.c1, "HobbyTypeId", "HobbyType"), "-- Select --")
#Html.ValidationMessageFor(model => model.HobbyType)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.HobbyDetails)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.HobbyDetails)
#Html.ValidationMessageFor(model => model.HobbyDetails)
</div>
</fieldset>
<p><input type="Submit" value="Create" /> </p>
}
Apparently i found that My Modelstate.IsValid is always false.....
since it stores only the HobbyId and getting Hobby Type as null the HobbyMasters hobbytype object.....
dnt knw where i'm going wrong may be in dropdownlist or something else.....
Plaese help me asap:(
There are a couple of issues with your code:
First is the fact that you decorated HobbyNames collection property with the [Required] attribute. You should use this attribute only on simple properties. In fact you could leave the property but it will have absolutely no effect
The second issue with your code is that you have decorated the HobbyType string property of the HobbyTypes model with a [Required] attribute but you never use this property in your view. So no value is sent when you submit the form and your model is invalid.
Another issue with your code is that you bound the dropdown list to the model => model.HobbyType.HobbyTypeId property. But the HobbyTypeId is not a nullable type. And yet you made your dropdown contain a default value: "-- Select --". This is not possible. If you want to have a dropdown list with an optional value you must bind it to a nullable property on your model.
I have tried to clean up your code a little.
Model:
public class HobbyMasters
{
public virtual int HobbyId { get; set; }
[Required]
public virtual string HobbyName { get; set; }
public virtual HobbyTypes HobbyType { get; set; }
[Required]
public virtual string HobbyDetails { get; set; }
public virtual ICollection<HobbyMasters> HobbyNames { get; set; }
}
public class HobbyTypes
{
[Required]
public virtual int? HobbyTypeId { get; set; }
public virtual string HobbyType { get; set; }
public virtual ICollection<HobbyTypes> Hobby { get; set; }
}
Controller:
public class HomeController : Controller
{
public ActionResult Create()
{
ViewBag.c1 = (ICollection<HobbyTypes>)(new Hobby_MasterService().GetAllHobbyTypes());
var model = new HobbyMasters();
return View(model);
}
//
// POST: /Hobbies/Create
[HttpPost]
public ActionResult Create(HobbyMasters hobby)
{
if (ModelState.IsValid)
{
try
{
new Hobby_MasterService().SaveOrUpdateHobby(hobby);
return RedirectToAction("Index");
}
catch (Exception ex)
{
ModelState.AddModelError("", ex.Message);
}
}
ViewBag.c1 = (ICollection<HobbyTypes>)new Hobby_MasterService().GetAllHobbyTypes();
return View(hobby);
}
}
View:
#model HobbyMasters
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<fieldset>
<legend>Hobby Master</legend>
<div class="editor-label">
#Html.LabelFor(model => model.HobbyName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.HobbyName)
#Html.ValidationMessageFor(model => model.HobbyName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.HobbyType)
</div>
<div class="Editor-field">
#Html.DropDownListFor(model => model.HobbyType.HobbyTypeId, new SelectList(ViewBag.c1, "HobbyTypeId", "HobbyType"), "-- Select --")
#Html.ValidationMessageFor(model => model.HobbyType.HobbyTypeId)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.HobbyDetails)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.HobbyDetails)
#Html.ValidationMessageFor(model => model.HobbyDetails)
</div>
</fieldset>
<p><input type="Submit" value="Create" /></p>
}
Also I would very strongly recommend you to use view models. Don't pass your domain entities to your views. Define view models.
How do i update a domain object with ViewModel with AutoMapper using Entity framework?
I have a View to edit a Question entity.
This is my Edit action:
public ActionResult Edit(int id)
{
var question = db.Question.Single(q => q.question_id == id);
Mapper.CreateMap<Question, EditQuestionViewModel>();
EditQuestionViewModel eqvm = Mapper.Map<Question, EditQuestionViewModel>(question);
eqvm.QuestionTypes = new SelectList(db.Question_Type, "type_code", "type_description", question.type_code);
eqvm.Categories = new SelectList(db.Category, "category_id", "category_name", question.category_id);
eqvm.Visibility = new SelectList(new Dictionary<int, string> {
{ 1, "Ja"},
{ 0, "Nej"}
}, "Key", "Value");
return View(eqvm);
}
And my ViewModel looks like this:
public class EditQuestionViewModel
{
public int question_id { get; set; }
public string question_wording { get; set; }
public bool visible { get; set; }
public int question_number { get; set; }
public string help_text { get; set; }
public Category Category { get; set; }
public Question_Type Question_Type { get; set; }
public string SelectedCategory { get; set; }
public string SelectedQuestionType { get; set; }
public SelectList Categories { get; set; }
public SelectList QuestionTypes { get; set; }
public SelectList Visibility { get; set; }
public string RefUrl { get; set; }
}
This is the View:
#using (Html.BeginForm("Edit", "AdminQuestion", FormMethod.Post))
{
#Html.ValidationSummary(true)
<fieldset>
<legend>Spørgsmål</legend>
<div class="editor-label">
#Html.LabelFor(model => model.question_wording, "Spørgsmål")
</div>
<div class="editor-field">
#Html.TextAreaFor(model => model.question_wording, new { #class = "required", rows = 3, cols = 50 })
#Html.ValidationMessageFor(model => model.question_wording)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.SelectedCategory, "Hvilken kategori tilhører dette spørgsmål?")
</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.SelectedCategory, Model.Categories)
#Html.ValidationMessageFor(model => model.SelectedCategory)
</div>
<div class="editor-label">
#Html.LabelFor(x => x.SelectedQuestionType, "Spørgsmålstype")
</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.SelectedQuestionType, Model.QuestionTypes)
#Html.ValidationMessageFor(model => model.SelectedQuestionType)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.visible, "Skal dette spørgsmål være synligt?")
</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.visible, Model.Visibility)
#Html.ValidationMessageFor(model => model.visible)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.question_number, "Hvilket nummer har spørgsmålet inden for sin kategori?")
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.question_number, new { #class = "required digits" })
#Html.ValidationMessageFor(model => model.question_number)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.help_text, "Hjælpetekst som hjælper brugeren med at forstå spørgsmålet:")
</div>
<div class="editor-field">
#Html.TextAreaFor(model => model.help_text, new { rows = 20, cols = 50 })
#Html.ValidationMessageFor(model => model.help_text)
</div>
<br />
<input type="submit" value="Gem" />
</fieldset>
How do i update the entity when i submit the form ?
How should the mapping between the ViewModel and EF Model look like, using AutoMapper?
The properties
public string SelectedCategory { get; set; }
public string SelectedQuestionType { get; set; }
In the ViewModel are supposed to be linked with category_id and type_code in the EF model
Also notice the property
public bool visible { get; set; }
I use BIT in my database. Will this work with the values 0 and 1, which is use in the SelectList?
Thanks!
you would need to get the object from entity framework, and then use automapper like this:
var item = repository.getbyid(model.Id);
_mappingEngine.Map(viewModel, item);
repository.save(item);
When you submit your form, you need to have an action on your controller that will handle the post to the server.
So in addition to the Edit action you currently have, you will need to have another action defined like so:
[HttpPost]
public ActionResult Edit(EditQuestionViewModel model)
{
//Do the mapping to from your ViewModel to the EF model here
return View();
}
What this does is sets up a handler so your form can post the data back to the controller and it will bind your fields on your form to the model parameter.
Once you have done this, you can simply map the model back to EF and persist it to the database.
Also, using a bool is perfectly valid and EF will translate and save it as a 0 or 1 in the database for you.
I've been reading all about UpdateModel() and custom model binders and i still cant figure this out. Seems like theres gotta be a simple answer to this.
I have a class called user that i user all over my MVC Web app.
public class User
{
[Key]
public int ID { get; set; }
[Required]
public string Name { get; set; }
[Required]
[Email]
public string Email { get; set; }
[Required]
public string Password { get; set; }
public virtual ICollection<User> WorkTasks { get; set; }
}
And then a WorkTask that has it in a few places:
public class WorkTask
{
[Key]
public int ID { get; set; }
[Required]
public string Name { get; set; }
public DateTime? DesiredStartDate { get; set; }
public DateTime? DesiredEndDate { get; set; }
public TimeSpan? DesiredTimeSpent { get; set; }
public virtual ICollection<WorkTaskTag> Tags { get; set; }
public virtual ICollection<WorkTaskPeriod> Periods { get; set; }
public virtual ICollection<User> InvolvedUsers { get; set; }
public virtual User CreatedBy { get; set; }
public virtual User AssignedTo { get; set; }
public string UserNameAssignedTo
{
get
{
if(AssignedTo!=null)
return AssignedTo.Name;
return CreatedBy.Name;
}
}
public string TotalTimeSpent {
get
{
var concretePeriods = Periods
.Where(i => i.StartDate.HasValue && i.EndDate.HasValue);
if (concretePeriods != null && concretePeriods.Count() > 0)
{
TimeSpan ts = new TimeSpan();
foreach (var p in concretePeriods)
{
var t=p.EndDate.Value-p.StartDate.Value;
ts.Add(t);
}
TimePeriodHelpers help = new TimePeriodHelpers();
return help.GetTimeFormat(ts);
}
return "0:00";
}
}
}
So how do i make a create template for this WorkTask that allows the User class to be bound to the WorkTask in multiple places?
Here's my very shoddy attempt:
[HttpPost]
public ActionResult Create(WorkTask worktask)
{
LoadUsers();
string assignedto=Request["AssignedTo"];
var user = db.Users.First(i => SqlFunctions.StringConvert((double)i.ID) == assignedto);
UpdateModel<User>(user);
if (ModelState.IsValid)
{
db.WorkTasks.Add(worktask);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(worktask);
}
and the view:
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<div class="editor-label">
#Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Name)
#Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.DesiredStartDate,"Desired Start Date")
</div>
<div class="editor-field">
#Html.EditorFor(model => model.DesiredStartDate)
#Html.ValidationMessageFor(model => model.DesiredStartDate)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.DesiredEndDate,"Desired End Date")
</div>
<div class="editor-field">
#Html.EditorFor(model => model.DesiredEndDate)
#Html.ValidationMessageFor(model => model.DesiredEndDate)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.DesiredTimeSpent,"Desired Time Spent")
</div>
<div class="editor-field">
#Html.EditorFor(model => model.DesiredTimeSpent)
#Html.ValidationMessageFor(model => model.DesiredTimeSpent)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.AssignedTo,"User Assigned To")
</div>
<div class="editor-field">
#Html.DropDownListFor(i=>Model.AssignedTo,(IEnumerable<SelectListItem>)ViewBag.Users)
#Html.ValidationMessageFor(model => model.AssignedTo)
</div>
<p>
<input type="submit" value="Create" />
</p>
Instead of trying to bind the nested User object, i made the form and controller based upon a ViewModel which just had the UserID. I then assumed that if the ViewModel validated correctly, than i can go ahead and persist the WorkTask and nested User objects.