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 effectThe second issue with your code is that you have decorated the
HobbyTypestring property of theHobbyTypesmodel 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.HobbyTypeIdproperty. But theHobbyTypeIdis 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:
Controller:
View:
Also I would very strongly recommend you to use view models. Don’t pass your domain entities to your views. Define view models.