-1

i have one dropdownlist this value coming from another table so in this dropdown selected value insert into another table

see our image

 <div class="col-lg-4">
                    <fieldset class="form-group">
                        @Html.LabelFor(model => model.CompanytypeID, new { @class = "form-label semibold" })
                        @Html.DropDownList("CompanyType", null, "--- Select CompanyType Name ---", new { @class = "select2-arrow" })
                        @Html.ValidationMessageFor(model => model.CompanytypeID, "", new { @style = "color:red" })
                    </fieldset>
                </div>



public void CompanyType_Bind()
        {
            DataSet ds = dDSP.Get_CompanyType();
            List<SelectListItem> companylist = new List<SelectListItem>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                companylist.Add(new SelectListItem { Text = dr["CompanyType"].ToString(), Value = dr["CompanytypeID"].ToString() });
            }
            ViewBag.CompanyType = companylist;
        }




public DataSet Get_CompanyType()
        {
            SqlCommand cmd = new SqlCommand("Select * From UserType", constr);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }

Error page

  • if it's not wrapped in a form with a submit button, then use ajax to submit the selected value on change of the dropdown list. you'll need to create a post controller action to receive the data posted from ajax. Then in this controller action, you will make a call to the db to save the value – GregH Oct 13 '17 at 14:14
  • share your complete cshtml code. I think you have not put the dropdown code inside the @Html.Beginform. – mzh Oct 13 '17 at 15:04

2 Answers2

0

You haven't tied your the dropdown for your code to any property of your Department model.

@Html.DropDownList("CompanyType", null, "--- Select CompanyType Name ---", new { @class = "select2-arrow" })

will generate a select element with the name CompanyType.

From your Validator code you can see that you want to tie it to CompanytypeID

Easiest thing to do would be to change you dropdown declaration to DropDownListFor and "bind" it to CompanytypeID

<div class="col-lg-4">
                    <fieldset class="form-group">
                        @Html.LabelFor(model => model.CompanytypeID, new { @class = "form-label semibold" })
                        @Html.DropDownListFor(m => m.CompanytypeID, ViewBag.CompanyType as IEnumerable<SelectListItem>, "--- Select CompanyType Name ---", new { @class = "select2-arrow" })
                        @Html.ValidationMessageFor(model => model.CompanytypeID, "", new { @style = "color:red" })
                    </fieldset>
                </div>

It would be good to see your entire cshtml page.

I'd also recommend using ViewModels. It looks like you are POST'ing back your entity model which is why your putting this list in a ViewBag.

Fran
  • 6,440
  • 1
  • 23
  • 35
0

Your SELECT element name should match with your property name. With your current code, you are generating a select element with name "CompanyType", But in your view model(Department) your property name is CompanyTypeId.

For model binding to work, the input element name should match with the property name. So use the same name for your SELECT element and you should be good.

@Html.DropDownListFor(x=>x.CompanyTypeId, ViewBag.CompanyType as List<SelectListItem>,
                      "Select CompanyType", new { @class = "select2-arrow" })

Going further, do not post link to images of your code. Instead include the relevant code in the question itself.

Shyju
  • 214,206
  • 104
  • 411
  • 497