0

Hi i am new to ASP.NET MVC. I am not sure how to deal with Check box or Radio Button to get values when they are clicked. Can any one help me? I am providing a simple code that might help you understand what i meant to be. Please share examples.

<script type="text/javascript" >
     function check(browser)
     {
           document.getElementById("answer").value=browser;
     } </script>

 <form action="">
         <input type="radio" name="browser"
 onclick="check(this.value)"
 value="Internet Explorer"/>Internet
 Explorer<br />
         <input type="radio" name="browser"
 onclick="check(this.value)"
 value="Firefox"/>Firefox<br />
         <input type="radio" name="browser"
 onclick="check(this.value)"
 value="Netscape"/>Netscape<br />
         <input type="radio" name="browser"
 onclick="check(this.value)"
 value="Opera"/>Opera<br />
         <br />
         Your favorite browser is: <input type="text" id="answer"
 size="20"/>  </form>

4 Answers 4

1

controller code

  public ActionResult Index()
  {
    ViewData["list"] = new[]
    {
      new SelectListItem {Text = "InternetExplorer", Value = "InternetExplorer"},
      new SelectListItem {Text = "Firefox", Value = "Firefox"},
      new SelectListItem {Text = "Safari", Value = "Safari"},
      new SelectListItem {Text = "Opera", Value = "Opera"}
    };

    return View();
  }

  [AcceptVerbs(HttpVerbs.Post),ActionName("Index")]
  public ActionResult IndexPost(string browser)
  {
    // ...
  }

view code

  <% using (Html.BeginForm()) { %>
    <% foreach(var item in (IEnumerable<SelectListItem>)ViewData["list"]) { %>

    <label>
    <% = Html.RadioButton("browser", item.Value) %>
    <% = item.Text %></label>
    <% } %>

    <input type="submit" value="Select" />
  <% } %>

  <script type="text/javascript" src="<% = Url.Content("~/Scripts/jquery-1.3.2.js") %>" ></script>
  <script type="text/javascript">
    $(function() {
      $("form:first").submit(function(e) {
        e.preventDefault();
        alert($(this).find(":radio:checked").val());
      });
    });
  </script>

If you want browser value in action, you coding in IndexPost method. or you want in javascript, onsubmit or onclick(and other) event handling, get checked radiobutton value at jQuery.

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

Comments

1

This was logic taken from: http://byatool.com/mvc/asp-net-mvc-how-to-handle-multiple-checkboxes-with-viewsactions-jquery-too/. I simply modified it very minimally.

----------
HTML Part|
----------
{form action="/Test/CheckForIds/" method="post"}
    {div}
        {input type="checkbox" name="IdList" value="1"  /}
        {input type="checkbox" name="IdList" value="2" /}
        {input type="checkbox" name="IdList" value="3" /}
        {input type="checkbox" name="IdList" value="4" /}
    {/div}
    {div}
        {input type="submit" value="go" /}
    {/div}
{/form}

----------------
Controller Part|
----------------

{AcceptVerbs(HttpVerbs.Post)} _
Function GroupPageSend(ByVal selectedObjects() As String) As ActionResult
    {!--- YOUR CODE GOES HERE ---}

    EX//
        For Each item In selectedObjects
            If i = 0 Then
                string = Trim(item)
                i = i + 1
            Else
                string = string & "," & Trim(item)
            End If
        Next
End Function

The above will gather values from selected checkboxes and will allow you to manage results.

Keep in mind all { = < and all } = >

Comments

0

I am probably not getting your question, but your sample will work well.

When you submit the form and the controller's method is called asp.net mvc will set the "browser" parameter to the value of the selected radio button's value.

Comments

0

hai friend try this,

<asp:RadioButton ID="RadioButton1" runat="server" onmousedown="yourjsfunc();" />

2 Comments

The code was inside angle brackets, but not in an SO code block.
This code will not work in ASP.Net MVC. It's only works with ASP.Net Web Forms