Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7697505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:01:48+00:00 2026-05-31T22:01:48+00:00

I am using nhibernate and mvc3 in asp.net I’m trying to add data into

  • 0

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:(

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:01:50+00:00Added an answer on May 31, 2026 at 10:01 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating an ASP.NET MVC3 application using NHIBERNATE. I have a base class
I am going to build my application using asp.net mvc3 and nhibernate 3.2. I
I have an asp.net-mvc3 website using nhibernate and SQL server. I have 2 web
Using: ASP.NET MVC3 Ninject 2 Fluent nHibernate I have 2 databases (DB1 & DB2).
I am working on a web-app with ASP.NET MVC3. We are using nHibernate with
I am using Ninject, NHibernate, ASP.NET MVC3 and repository pattern. The module binding in
Using NHibernate.Mapping.Attributes, I have a entity class with something like: [Class] public class EntityA
I'm using NHibernate 3.2 and I have a repository method that looks like: public
I'm fighting with a strange behavior here... Doing an asp.net mvc3 application with NHibernate
I am using NHibernate in a project, and I am trying to add it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.