0

I am have filled a ViewData["ParamTypes"] object with a selectlist items and now using it in dropdownlistfor so upon submitting a form a value being selected in the list should also be sent to the string parameter ParamNo but it doesn't work. It says the paramNo should also be of selectlistitem type.

 @Html.DropDownListFor(model => model.ParamNo, (IEnumerable<SelectListItem>) ViewData["ParamTypes"] )

Controller:

  [HttpGet]
        public ActionResult DoubleEntryVoucher() //
        {
            try
            {
                clsDoubleEntryViewModel v = new clsDoubleEntryViewModel();
                v.DetailsList = new List<clsDoubleEntryViewModel>();
                ViewData["ParamTypes"] =
                        new List<SelectListItem>() 
                        {
                            new SelectListItem{Text= "All", Value="0"},
                            new SelectListItem{Text= "Invoice No", Value="0"},
                            new SelectListItem{Text= "ChequeNo", Value="0"},
                            new SelectListItem{Text= "Voucher No", Value="0"},
                        };
                v.Role_ID = LoginUserRoleID;
                return View(v);
            }
            catch (Exception ex)
            {
                ShowMessage(MessageBox.Error, OperationType.Error, ex.Message);
                return View();
            }

        }

Model:

   public string ParamNo { get; set; }

view:

@using (Html.BeginForm())
{
    //@Html.AntiForgeryToken()
    <div class="row">
        <div class="col-md-12">
            <div class="box box-primary">
                <div class="box-body">
                    <div class="col-md-4">
                        <div class="form-group">
                            @Html.Label("Parameter Type", htmlAttributes: new { @class = "control-label" })
                            @Html.DropDownListFor(model => model.ParamNo, (IEnumerable<SelectListItem>) ViewData["ParamTypes"] )
                        </div>
                    </div>

                    <div class="col-md-4">
                        <div class="form-group">
                            @Html.Label("Param", htmlAttributes: new { @class = "control-label" })
                            @Html.EditorFor(Model => Model.ParamNo, new { htmlAttributes = new { @class = "form-control" } })
                        </div>
                    </div>
                </div>
                <div class="box-footer">
                    <input type="submit" value="Search" class="btn btn-success  btnMarginRight pull-right" />
                </div>
            </div>
        </div>
    </div>
  • Show the relevant code including your model and controller methods –  Jun 26 '18 at 07:03
  • @StephenMuecke: updated, check –  Jun 26 '18 at 07:42
  • You still did not show the relevant code (the POST method),but read the dupe to understand the issue and how to solve it (and STOP using `ViewData`!) –  Jun 26 '18 at 07:44
  • @StephenMuecke actually there's not post code at view level, i am usng beginform –  Jun 26 '18 at 07:49
  • @StephenMuecke: check the updated code –  Jun 26 '18 at 07:50
  • `BeginForm()` has nothing to do with it. Read the dupe - it means that `ViewData["ParamTypes"]` is `null`! –  Jun 26 '18 at 07:51
  • so what should I use? –  Jun 26 '18 at 07:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173800/discussion-between-stephen-muecke-and-stacky). –  Jun 26 '18 at 07:59

0 Answers0