Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET MVC validation Razor , +
Hi,
 
I am using ASP.NET, MVC 3, Razor
I use Remote for server validation. My problem is the checked value of RadioButton is always the value of the first RadioButton
 
My Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
 
namespace AdminSite.Models
{
    public class GroupAndCM
    {
        [Key]
        public int GroupAndCMID { get; set; }
        
        [Display(Name = "Group")]
        [Required(ErrorMessage = "Group is required.")]
        [Remote("ValidateGroupAndCM", "AccessCtrl", AdditionalFields = "selectedCMID", HttpMethod = "Post", ErrorMessage = "Selected CM is set for this Group.")]
        public int? GroupID { get; set; }
 
        [Display(Name = "CM")]
        [Required(ErrorMessage = "CM is required.")]
        public int CMID { get; set; }
 
        public virtual Group Group { get; set; }
        public virtual CM CM { get; set; }
    }
}
 

 
My View
 
<div class="editor-field">
            <table style="width:100%;">
                <tr>
                    @{
                        int cnt = 0;
                        foreach (AdminSite.Models.CM cm in ViewBag.CMs)
                        {
                            if (cnt++ % 3 == 0)
                            {
                                @: </tr> <tr>
                            }
                            @:<td>
                                @Html.RadioButton("selectedCMID", cm.CMID) @cm.CMName
                                
                            @:</td>
                        }
                        @:</tr>
                    }
            </table>
        </div>
 
@Html.DropDownListFor(model => model.GroupID, (SelectList)ViewBag.Groups, string.Empty, new { @style = "width:220px;" })
        @Html.ValidationMessageFor(model => model.GroupID)
 
 
And here is my Controller
 
public ActionResult ValidateGroupAndCM(int groupID, int? selectedCMID)
        {
            bool flag = true;
            int t = (int)selectedCMID;
            var query = from gc in db.GroupAndCMs
                        where gc.GroupID == groupID && gc.CMID == t
                        select gc;
            if (query.SingleOrDefault() != null)
            {
                flag = false;
            }
 
            return Json(flag, JsonRequestBehavior.AllowGet);
        }
 

When user select a value from Group (DropDown), it will trigger then function ValidateGroupAndCM of controller:
 
public ActionResult ValidateGroupAndCM(int groupID, int? selectedCMID)
 
The value of "groupID" is OK, exactly as seleted value
but the "selectedCMID" is wrong, it is always return the value of the first RadioButton although the second RadioButton is selected.
 
This is the view rendered:
 
<input id="selectedCMID" name="selectedCMID" type="radio" value="1"> RadioButton1
<input id="selectedCMID" name="selectedCMID" type="radio" value="2"> RadioButton2
 
 
Please help!
 
Sorry for my english Smile | :)
Posted 20-Sep-12 0:51am
Comments
Thanh Hoang at 1-Oct-12 22:12pm
   
Hello, anybody... help...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 20 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100