user2501937
user2501937

Reputation: 31

How to assign a value to model property once HTML.RadioButtonFor is selected

I am new to MVC. Currently I have a strongly typed view, and I use two Html.RadioButtonFor for labels, the codes are somethings like:

<label class="radio">
       @Html.RadioButtonFor(model => model.Incoming, true)IncomingItems</label>
 <label class="radio">
       @Html.RadioButtonFor(model => model.Incoming, false)OutgoingItems</label>
 <input type="hidden" id="isIncomingItem" value = "@Model.Incoming" />

What I want is to set model.Incoming to be true when the first radio button is selected or set it to false, and then I want to use this model property right away in the view( assign this value to "isIncomingItem") before the entire form is submitted to server.

Do you guys have a idea about how I could achieve it. Really appreciated!

Upvotes: 3

Views: 1371

Answers (2)

Chamika Sandamal
Chamika Sandamal

Reputation: 24292

Your Incoming property is already filled with the selected value and it will be available for the action method. And if you want to set the same value in the hidden field, try as following

@Html.HiddenFor(m => m.Incoming,new { id = "isIncomingItem"})

Upvotes: 1

Mathew Thompson
Mathew Thompson

Reputation: 56429

You'd have to use jQuery, doing something like:

$("[name='Incoming']").change(function () {
    $("#isIncomingItem").val($(this).val());
});

Upvotes: 0

Related Questions