views:

554

answers:

5

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>
A: 

hai friend try this onmousedown="yourjsfunc();" in ur radio button

Pandiya Chendur
A: 

hai friend try this,

<asp:RadioButton ID="RadioButton1" runat="server" onmousedown="yourjsfunc();" />
Pandiya Chendur
um... answer is...?
Daniel A. White
The code was inside angle brackets, but not in an SO code block.
R. Bemrose
This code will not work in ASP.Net MVC. It's only works with ASP.Net Web Forms
Chuck Conway
A: 

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.

eglasius
A: 

Well, I missed to tell that i am using Html helpers. I don't know how to use the same code above with Html Helpers. Please i am waiting your answer.

Thanks for replying

+1  A: 

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.

takepara
+1 The above code looks like it should work.
Chuck Conway