can not add data from dropdown to table - asp.net-mvc-3

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.

Related

How to bind dropdown list from action method in MVC 3 inside create view

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")

"Null" Foreign key relationship creating record on MVC3 + EF

I'm using EF + MVC3 + razor
I have my userProfile model class with a Name, Surname variables + a foreign key Hobby (as I want to see it as a dropdown)
I created a controler simply selecting this clase and Data Context.
When I try to create the record, it doesn’t let me, as it says in the validation area: "The Hobby field is required. " .
I want the "Hobby" dropdown not to be required.
How can I acomplish that?!..
This is what I have:
Model:
public class UserProfile {
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Surname { get; set; }
public int HobbiesId { get; set; }
public virtual Hobby Hobby { get; set; }}
public class Hobby
{
public int HobbiesId { get; set; }
public string HobbieName { get; set; }
public virtual ICollection<UserProfile> UserProfiles { get; set; }
}
public class UserProfileDBContext : DbContext
{
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<Hobby> Hobbies{ get; set; }
}
View:
[..]#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>UserProfile</legend>
<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.Surname)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Surname)
#Html.ValidationMessageFor(model => model.Surname)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.HobbyId, "Hobby")
</div>
<div class="editor-field">
#Html.DropDownList("HobbyId", String.Empty)
#Html.ValidationMessageFor(model => model.HobbyIdId)
</div>
[..]
The controller's create is:
[HttpPost]
public ActionResult Create(UserProfile userprofile)
{
if (ModelState.IsValid)
{
db.UserProfiles.Add(userprofile);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.HobbyId = new SelectList(db.Hobbies, "HobbyId", "Hobby", userprofile.HobbyId);
return View(userprofile); }
I already tried modifying:
public class Hobby
{
public int ?HobbiesId { get; set; }
but it didn't work.
Any help?..
Thanks in advance!..
PnP
Add a foreign key constraint as shown below:
public int? HobbiesId { get; set; }
[ForeignKey("HobbiesId")]

Uploading and converting an image/file into a mssql databse

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

How to update a domain object with ViewModel and AutoMapper using Entity framework?

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.

How to bind a nested model in MVC3?

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.

Resources