views:

7

answers:

0

I have pasted all the code below. The problem is when I try to save or update Town Entity, County Title comes null and Model State becomes false. Could you suggest me a way to get complete county Object here while saving Town. Could you also suggest me better flent nHibernate mapping in this case, please?

I have following classes and mapping.

public class County
 {
        public virtual Int32 Id { get; set; }
        public virtual String Title { get; set; }
        public virtual IList<Town> Town { get; set; }
        public County()
        {
            Town = new List<Town>();
        }
 }



public class Town
 {
        public virtual Int32 Id { get; set; }
        public virtual String Title { get; set; }
        //public virtual Int32 CountyId { get; set; }
        public virtual County County { get; set; }
 }

public class CountyMap : ClassMap<County>
{
   public CountyMap()
   {
      Table("County");
      Id(x => x.Id).GeneratedBy.Identity();
      Map(x => x.Title);
      HasMany<Town>(x => x.Town).KeyColumns.Add("CountyId").Cascade.None();
   }
}


public class TownMap : ClassMap<Town>
{
   public TownMap()
   {
      Table("Town");
      Id(x => x.Id).GeneratedBy.Identity();
      Map(x => x.Title);
      References(x => x.County, "CountyId").Cascade.None().Fetch.Join();
   }
}

This is edit method in TownController.cs

[Authorize]
 [AcceptVerbs(HttpVerbs.Get)]
 public ActionResult Edit(int id)
 {
    ViewData["CountyData"] = GetAllCounty();
    townsRepository = new TownsRepository();
    Town town = townsRepository.GetById(id);
    return View(town);
 }

and this is save method in the above controller.

 [AcceptVerbs(HttpVerbs.Post)]
 [ValidateInput(false)]
 [ValidateAntiForgeryToken]
 public ActionResult Save(Town towns, int id)
 {
     towns.Id = id;
     //towns.County = (new CountyRepository()).GetById(towns.County.Id);
     if (ModelState.IsValid)
     {
        townsRepository = new TownsRepository();
        if (id > 0)
        {
           townsRepository.Update(towns);
        }
        else
        {
           townsRepository.Save(towns);
        }
        return RedirectToAction("Index");
     }
     else
     {
        ViewData["CountyData"] = GetAllCounty();
        return View("Edit", towns);
     }

}

This is my view:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage<Core.Domain.Model.Town>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<% Html.EnableClientValidation(); %>
      <% using (Html.BeginForm("Save", "Town" ))
           {
               //CountyRepository countiesRepository = new CountyRepository();
               %>
        <%= Html.AntiForgeryToken()%>
        <%= Html.HiddenFor(model => model.Id)%>
        <p>Title : <%= Html.TextBoxFor(model => model.Title)%>
                        <%=Html.ValidationMessageFor(model => model.Title, null, new { @class = "error" })%></P>
          <p> County :
                        <%= Html.DropDownListFor(model => model.County.Id,  new SelectList((IEnumerable)ViewData["CountyData"], "Id", "Title", Model.County.Id), "--- select ---")%>
           </p>
    <p>
                        <input type="submit" value="Save" />
         </p>
      <%} %>
        <div>
            <%=Html.ActionLink("Back to List", "Index") %>
        </div>
    </div>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="HeaderContent" runat="server">
</asp:Content>