[RESOLVED]error passing dropdown selected value from view to controller

• in Controller :

        public ActionResult RegisterKarjoo()
        {
            ViewBag.ReshtehList = new SelectList(db.Reshtehs, "ReshtehID", "ReshtehTitle");
            ViewBag.MaqtaList = new SelectList(db.Maqtas, "MaqtaID", "MaqtaTitle");
            ViewBag.OstanList = new SelectList(db.Ostans, "OstanID", "OstanTitle");
            return View();
        }

        [HttpPost]
        public ActionResult RegisterKarjoo(Karjoos karjoo, string ReshtehList, string MaqtaList, string OstanList)
        {
            if (ModelState.IsValid)
            {

            }
            return View(karjoo);
        }

• in View :

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" })) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true)
      
        <div class="form-group">
            @Html.LabelFor(model => model.ReshtehID, "رشته تحصیلی", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("ReshtehList", "--select--")
                @Html.ValidationMessageFor(model => model.ReshtehID)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.MaqtaID, "مقطع تحصیلی", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("MaqtaList", "--select--")
                @Html.ValidationMessageFor(model => model.MaqtaID)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.OstanID, "استان", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("OstanList", "--select--")
                @Html.ValidationMessageFor(model => model.OstanID)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.KarjooName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.KarjooName)
                @Html.ValidationMessageFor(model => model.KarjooName)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email)
                @Html.ValidationMessageFor(model => model.Email)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Password)
                @Html.ValidationMessageFor(model => model.Password)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Portfolio, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Portfolio)
                @Html.ValidationMessageFor(model => model.Portfolio)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Tel, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Tel)
                @Html.ValidationMessageFor(model => model.Tel)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Sex, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Sex)
                @Html.ValidationMessageFor(model => model.Sex)
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="ایجاد" class="btn btn-default" />
            </div>
        </div>
    </div>
}

• Error in browser :

browser does not send all data from dropdown

behzad alizadeh

[HttpPost]
        public ActionResult RegisterKarjoo(Karjoos karjoo, string ReshtehList, string MaqtaList, string OstanList)
        {
            if (ModelState.IsValid)
            {

            }
 ViewBag.ReshtehList = new SelectList(db.Reshtehs, "ReshtehID", "ReshtehTitle");
            ViewBag.MaqtaList = new SelectList(db.Maqtas, "MaqtaID", "MaqtaTitle");
            ViewBag.OstanList = new SelectList(db.Ostans, "OstanID", "OstanTitle");



return View(karjoo); }

behzad alizadeh

@Html.DropDownList("ReshtehList", "--select--")

try this

@Html.DropDownList("ReshtehID",ViewBag.ReshtehList as SelectList,"--select--")

on post action

        [HttpPost]
        public ActionResult RegisterKarjoo(Karjoos karjoo, string ReshtehID, string MaqtaList, string OstanList)
        {
            if (ModelState.IsValid)
            {

            }
            return View(karjoo);
        }

Hi behzad,

Try this in your view for the DropDownList

@Html.DropDownList("ReshtehID", (SelectList)ViewBag.ReshtehList, "Select")

Hope This Helps

Smile so thanks for all…. worked

Leave a Reply