开发者

MVC 2 - two radio button group persist state

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 i开发者_Python百科t at client side. Any ideas on how to fix it would be much appreciated.

Thanks, Raja


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);
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜