0

I have the following ViewModel:

namespace SimpleModel.ViewModels
{
    public class ClientSearch
    {
       public int Type { get; set; }
       public string String { get; set; }
    }
}

The following is the HTML code snippet:

<div id="clientSelect">
    <input type="radio" name="clientSelect" value="1" />Account Number<br />
    <input type="radio" name="clientSelect" value="2" />ID Number / Company Number<br />
    <input type="radio" name="clientSelect" value="3" />Surname / Company Name<br />
    @Html.EditorFor(model => model.String)
</div>

<p>
    <input type="submit" value="Search" onclick="clientSearch('clientSelect')" />
</p>

I have the following JavaScript function:

<script type="text/javascript">
    function clientSearch(strGroupName) {
        var selectedValue = 0;
        var arrInputs = document.getElementsByTagName("input");
        for (var i = 0; i < arrInputs.length; i++) {
            var oCurInput = arrInputs[i];
            if (oCurInput.type == "radio" && oCurInput.name == strGroupName && oCurInput.checked)
                selectedValue = oCurInput.value;
        }
     }
</script>

I need to update ClientSearch model Type field with selectedValue from within the Javascript function so I may pass the model back to the Controller for processing.

Any help would be appreciated.

1
  • 1
    are you ok with using jQuery in your code? Commented Dec 4, 2013 at 10:15

1 Answer 1

1

First of all this object is not ok, you can not have a property that is a c# keyword

public class ClientSearch
{
   public int Type { get; set; }
   public string String { get; set; } // This right here is a reserved c# keyword
}

So change your ClientSearch class to something like

public class ClientSearch
{
   public int Type { get; set; }
   public string SearchString { get; set; }
}

Then your View will look something like:

<div id="clientSelect">
    @Html.RadioButtonFor(x => x.Type, 1) @:AccountNumber<br/>
    @Html.RadioButtonFor(x => x.Type, 2) @:ID Number / Company Number<br/>
    @Html.RadioButtonFor(x => x.Type, 3) @:Surname / Company Name<br />
    @Html.TextBoxFor(model => model.SearchString)
</div>

<p>
    <input type="submit" value="Search" />
</p>

No javascript needed... imagine that :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much Scott - a great help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.