Nouman Arshad
Nouman Arshad

Reputation: 89

MVC model is null on post?c#

My view code of GNController

     public ActionResult Index()
    {
        if (Session["pUser_Name"] == null)
        {
            return RedirectToAction("Login", "Login");
        }
        ViewBag.pUser_Name = Session["pUser_Name"].ToString();


        DataSet ds_1 = Fill_Combo();

        ADO_Net_MVC.Models.GN.GN clsObj = new Models.GN.GN();
        clsObj.Branch_SName = GENERIC.ToSelectList(ds_1.Tables[0], "Branch_SName", "Branch_Code");

        clsObj.Branch_FY = GENERIC.ToSelectList(ds_1.Tables[1], "FY", "FYDates");
        Session["pLast_IPAddress"] = ds_1.Tables[3].Rows[0]["User_IP"].ToString();
        clsObj.Branch_DeptD = GENERIC.ToSelectList(ds_1.Tables[4], "Dept_DD", "Dept_ID");
        return View(clsObj);
    }

view binded as

@model ADO_Net_MVC.Models.GN.GN

@using (Html.BeginForm())
{
@Html.ValidationSummary(true)

 <div class="editor-field">

  @Html.DropDownList("cmbLocation", Model.Branch_SName)
   </div>
 @*    fill all drop down *@

  <input value="Continue" id="cmd_continue" class="btnBG_Green" type="submit"   tabindex="4" />

on post request the model is empty infact i want all drop downs selected value and text feilds

 [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(ADO_Net_MVC.Models.GN.GN model)
    {

     return View(model);
   } 

here is model

   public class GN
{

    public SelectList Branch_SName { get; set; }
    public SelectList Branch_DeptD { get; set; }
    public SelectList Branch_AcdYear { get; set; }
    public SelectList Branch_FY { get; set; }


}

can any body tell me why model is null and i want to get selected feild value and text How?

Upvotes: 1

Views: 1265

Answers (2)

Power Star
Power Star

Reputation: 1894

I will show only for first drop down. You apply this for all. You need to add some property in model.

public class GN
{
    public SelectList Branch_SName { get; set; }
    public string SelectedBranchName {get;set; }
}

And in View

@Html.DropDownListFor(m => m.SelectedBranchName , Model.Branch_SName)

Thats it!!!

Upvotes: 1

AGB
AGB

Reputation: 2448

Your model needs to contains properties which can map to the names of inputs in your form. The easiest way to do this is to use the helper methods such as Html.DropDownListFor and Html.TextBoxFor.

For your selects you need a property on the model which will store the selected value e.g.

public string CmbLocation { get; set; }

Then in your view you can use:

@Html.DropDownListFor(m => m.CmbLocation, Model.Branch_SName)

Now when you post your form you should see the value of the dropdown bound to the model.

Upvotes: 0

Related Questions