views:

94

answers:

1

Hi Guys,

I have two radio button groups in my partial view and based on the selections of two I have to manipulate an "Extension Area (div)". The requirement is to default the radio button groups to first ones by default and show the Extension Area relevant to it. The problem is when there is a validation error I am not able to persist the radio button groups. It changes to default since I am handling it at client side. Any ideas on how to fix it would be much appreciated.

Thanks, Raja

+1  A: 

Are you validating client side using MVC's ValidationMessage helper or validating in the controller ? If you post the page and validate using ModelState.IsValid at the controller, make sure that you return View with the same entity. If you are validating at the client side, can you just post some sample HTML code for the radio buttons?

Are you setting the default radio button setting on document.ready(...) or load(...) at the client side using JQuery/Javascript? Otherwise I have tested the following, it is working fine and the radio button state is persisting:

Model:

 public class MyModel
    {
        [Required(ErrorMessage="Name is required")]
        public string Name { get; set; }
        public bool IsSelected { get; set; }
    }

View:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TestSample.Models.MyModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%Html.BeginForm(); %>
<%Html.EnableClientValidation(); %>
        <p>
        <%=Html.RadioButtonFor(m=>m.IsSelected,"true") %>
        <%=Html.RadioButtonFor(m=>m.IsSelected,"false") %>
        <%=Html.TextBoxFor(m => m.Name) %>
        <%=Html.ValidationMessageFor(m => m.Name, "Required") %>
        <input type="submit" id="btnSubmit" name="name" value="Submit" />
        </p>
<%Html.EndForm(); %>
    <script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
      </asp:Content>

Controller:

[HttpPost]
public ActionResult Index(MyModel model)
  {
       if (ModelState.IsValid)
       {//Your code goes here }
       return View(model);
  }
Siva Gopal
Thanks for the reply Siva :-). I am using ModelState.IsValid at the controller and I am returning the same entity. The problem is all other controls are fine (Textboxes, dropdownlist) except for radio button group.
Raja
Please check my updated answer, once.
Siva Gopal
Let me know, if you still have problem. :-)
Siva Gopal