Click here to Skip to main content
15,886,075 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am new in Mvc. I have created a registration Page.Code is given below.
But in [httppost] Action create model returned is null. I do not understand why it returns null.

C#
public class MemberModel
    {
        SalutationRepository _salutationRepository = new SalutationRepository();
        CountryRepository _countryRepository = new CountryRepository();
        public tbl_Member tblMember { get; private set; }
        public SelectList Countries { get; private set; }
        public string ICountryCode { get; private set; }       
        public virtual string ICountry_id { get; set; }        
        public virtual string ISalutation_Id { get; set; }
        public SelectList Salutations { get; private set; }
        public List<SelectListItem> Genders { get; private set; }
        
        public MemberModel(tbl_Member _tblMember)
        {
            tblMember = _tblMember;
            Countries = GetCountries();
            ICountryCode = "";
            Salutations = GetSalutation();
            Genders = new List<SelectListItem>();
            Genders.Add(new SelectListItem { Text="Male",Value="Male"});
            Genders.Add(new SelectListItem { Text = "Female", Value = "Female" });
        }

        public SelectList GetCountries()
        {
            IEnumerable<SelectListItem> CountryList = _countryRepository.CopuntrySelectAll().ToList().Select(d => new SelectListItem() { Text = d.CountryName, Value = d.Country_id.ToString() });
            return new SelectList(CountryList, "Value", "Text", ICountry_id);
        }

        public SelectList GetSalutation()
        {
            IEnumerable<SelectListItem> SalutationList = _salutationRepository.SalutationSelectAll().ToList().Select(d => new SelectListItem() { Text = d.Salutation, Value = d.Salutation_Id.ToString() });
            return new SelectList(SalutationList, "Value", "Text", ISalutation_Id);
        }


        public string GetCountryCode(string ICountry_id)
        {
            string CountryCode="";
            if (!string.IsNullOrEmpty(ICountry_id))
            {
                int Country_id = Convert.ToInt32(ICountry_id);
                 CountryCode = _countryRepository.CountrySelect_Country_id(Country_id).CountryCode;
            }
            return CountryCode;
        }
    }

in controller

public ActionResult Create()
        {
            tbl_Member _tblMember = new tbl_Member();
            
            return View(new MemberModel(_tblMember));
        }

        [HttpPost]
        public ActionResult Create(tbl_Member _memberModel)
        {
            tbl_Member _tblMember = new tbl_Member();
            if (ModelState.IsValid)
            {
                _tblMember.Salutation_Id =Convert.ToInt32( _memberModel.Salutation_Id);
                _tblMember.FirstName =_memberModel.FirstName;                
                _memberRepository.MemberAdd(_tblMember);
                return RedirectToAction("List");
            }
            return View();
        }

in view

@model NewGlobalShoppers.Models.MemberModel
@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script src="../../Scripts/modernizr-1.7.js" type="text/javascript"></script>
<script src="../../Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
<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>MemberModel</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Salutations)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.tblMember.Salutation_Id, Model.Salutations, "Select")
            @Html.ValidationMessageFor(model => model.ISalutation_Id)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.tblMember.FirstName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.tblMember.FirstName)
            @Html.ValidationMessageFor(model => model.tblMember.FirstName)
        </div>        
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>
Posted
Updated 16-Jun-14 23:23pm
v4
Comments
ssharma20889 18-Jun-14 8:11am    
i think it is because you are using 'MemberModel' in view and in controller you are accessing 'tbl_Member'. i am not sure about it, just guessing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900