Divya
Divya

Reputation: 1213

Using DropdownListFor - Value is being set using Jquery, Displayed in Console too, but Null at Controller

Here is the Syntax of My Dropdown.

@Html.DropDownListFor(model => model.DealerIdRef, Model.Ddllist, " Select Dealer ", new { @class = "form-control"})

i want its default selected value and make it read-only so that user can not update selection. For this i'm using Jquery.

$('#DealerIdRef').val('@Session["MyID"]').trigger('change');
$("#DealerIdRef").attr('disabled', 'true');

this is setting the value and also exists in Console image

At Controller it is still null image2 image3

Edit

image4

if i'm making some mistake then please help. thanks in advance

Upvotes: 2

Views: 159

Answers (2)

user3559349
user3559349

Reputation:

Your javascript is setting the disabled attribute of the dropdownlist. Disabled form controls do not submit a value so the value of DealerIdRef in your model is its default (i.e. null because its int?).

If you want the value of the dropdownlist to be submitted, do not disabled it.

But based on i want its default selected value and make it read-only so that user can not update selection, then there is no point generating a dropdownlist, and in anycase, you set selected option by setting the value of the property your binding to. That is you set the value of DealerIdRef in the GET method before you pass the model to the view.

Since all you want is to display a value and have it posted back, then include a hidden input for the value and display the text

@Html.HiddenFor(m => m.DealerIdRef)
<div>...the txt you want to display...</div>

There is no point degrading performance by generating a SelectList and extra html when its not needed.

As a side note, your POST method would have throw this exception because you have not repopulated the SelectList in the POST method before you return the view.

Upvotes: 2

D-Shih
D-Shih

Reputation: 46239

I wrote a simple mock your question.

It can work. The simple code is on DropDownController

Here is the Source Code,I Upload to github.

ViewModel

public class DropDownViewModel
{
    [Display(Name = "Dealer")]
    public int? DealerIdRef { get; set; }

    public IEnumerable<SelectListItem> Ddllist { get; set; }
}

Index View

Mock Your Submit action

@model Sample.Models.DropDownViewModel

@using (Html.BeginForm("ShowDDL", "DropDown", FormMethod.Post))
{
    @Html.DropDownListFor(model => model.DealerIdRef, Model.Ddllist, " Select Dealer ", new { @class = "form-control" })
    <button>Submit</button>
}

ShowDDL View

Show your select data.

@model Sample.Models.DropDownViewModel

<b>Your Select Value: </b> @Model.DealerIdRef

DropDownController

public ActionResult Index()
{
    DropDownViewModel model = new DropDownViewModel()
    {
        Ddllist = GetDDL(),
        DealerIdRef = 1
    };

    return View(model);
}

[HttpPost]
public ActionResult ShowDDL(DropDownViewModel viewModel)
{
    return View(viewModel);
}

private IEnumerable<SelectListItem> GetDDL()
{
    return new List<SelectListItem>()
    {
        new SelectListItem()
        {
            Text = "One",
            Value = "1"
        },
        new SelectListItem()
        {
            Text = "Two",
            Value = "2"
        }
    };
}

Upvotes: 1

Related Questions