Remote Validation in ASP.Net MVC 3: How to use AdditionalFields in Action Method

前端 未结 3 999
慢半拍i
慢半拍i 2020-11-28 07:26

I\'ve been using the new ASP.Net MVC 3 RemoteAttribute to send a remote call to an action method that had a single parameter. Now I want to pass in a second parameter using

3条回答
  •  伪装坚强ぢ
    2020-11-28 08:12

    Strange. It works for me:

    Model:

    public class MyViewModel
    {
        [Required]
        [Remote("IsEmailAvailable", "Home", AdditionalFields = "InitialEmail")]
        public string Email { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new MyViewModel());
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            return View(model);
        }
    
        public ActionResult IsEmailAvailable(string email, string initialEmail)
        {
            return Json(false, JsonRequestBehavior.AllowGet);
        }
    }
    

    View:

    @model AppName.Models.MyViewModel
    @{
        ViewBag.Title = "Home Page";
    }
    
    
    @using (Html.BeginForm())
    {
        @Html.TextBoxFor(x => x.Email)
        @Html.ValidationMessageFor(x => x.Email)
        
        
    }
    

    IIRC there was some bug in ASP.NET MVC 3 RC2 with this remote validation that was fixed in the RTM.

提交回复
热议问题