How to pass model to controller using razor dropdownlist

I have a control DropDownListFor

that I want to show a display value that is in a property in a model / class (this is a class Rule

.) A view model is actually a collection of these models / classes. However, when I select an item from the DropDownList, I want to send back the entire model as a parameter. It works fine for me with the following code, but the Name property in the parameter is returned as null. Other properties have corresponding values.

View code:

@model List<StockTrader.Web.Data.Rule>

@{
    ViewBag.Title = "Configure Rules";
}

<h2>@ViewBag.Title</h2>

<h4>Choose a rule to edit:</h4>
<form method="post" id="rulesform" action="SaveRules">

@Html.DropDownListFor(m => m.First().RuleID, new SelectList(Model.AsEnumerable(), "RuleID", "Name"))

<div style="margin-bottom: 15px;">
    <label>Value:</label><br />
    <input type="number" name="Value" style="margin-bottom: 15px;" /><br />
    <button>Save Value</button>
</div>

      

Controller code:

public ActionResult SaveRules(Rule model)
{
    //do something
}

      

Rule class:

public class Rule
{
    public int RuleID { get; set; }

    public string Name { get; set; }

    public int Value { get; set; }

    public bool IsDeleted { get; set; }
}

      

We have Kendo controls, so if other controls would be more appropriate this is an option.

I would be happy to provide more code or information you may need.

Any thoughts or ideas?

EDIT:

So it turns out that this is what I needed to do, the accepted answer led me to this question, so I'm going to leave it checked.

View code (w / script included):

@Html.DropDownListFor(m => m.First().RuleID, new SelectList(Model.AsEnumerable(), "RuleID", "Name"), new { id = "ruleid", @onchange = "CallChangefunc(this)" })
@Html.HiddenFor(m => m.First().Name, new { id = "rulename" })

function CallChangefunc(e) {
    var name = e.options[e.selectedIndex].text;
    $("#rulename").val(name);
}

      

+3


source to share


2 answers


To do this you need a hidden field and use the dropdown in the client side change event to update the hidden field:

@Html.DropDownListFor(m => m.First().RuleID, new SelectList(Model.AsEnumerable(), "RuleID", "Name"),new { id= "ruleid" })
@Html.HiddenFor(m=>m.First().Name,new { id="rulename" })

      

and the jquery code:



$("#ruleid").change(function(){

  $("#rulename").val($(this).text());

});

      

The second option isif the Rule

collection comes from the database, you can get the RuleName by id by querying the db in the action.

+2


source


this can be achieved with UIHint

In the model class, in the property, RuleID

add an annotation for UIHint

. It basically allows you to render a partial (cshtml) for a property. So, on a partial basis, you can have a template to generate dropdwon with the style you want. When the page is generated. Now you can use the same Html.DropDownListFor

for RuleID

and the UI creates a dropdown for it.



This will avoid getting extra jQuery code to get the dropdown value, and the code is more concise and testable.

0


source







All Articles