Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc 3 remote attribute passing 3 fields

I want to pass three field to my controller using RemoteAttribute. How can i do it?

 public int ID1 { get; set; }  
 public int ID2 { get; set; }  

 [Remote("CheckTopicExists", "Groups", AdditionalFields = "ID1", ErrorMessage = " ")]  
 public string Topic { get; set; }   

        public ActionResult CheckTopicExists(string topic, int ID1,int ID2)   
        {
            return Json(true, JsonRequestBehavior.AllowGet);
        }

How can i pass three field to that function?

like image 267
Avinash Avatar asked Jun 20 '11 05:06

Avinash


1 Answers

You could separate them by comma:

AdditionalFields = "ID1, ID2"

Full example:

Model:

public class MyViewModel
{
    public int ID1 { get; set; }
    public int ID2 { get; set; }

    [Remote("CheckTopicExists", "Home", AdditionalFields = "ID1, ID2", ErrorMessage = " ")]
    public string Topic { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel
        {
            ID1 = 1,
            ID2 = 2,
            Topic = "sample topic"
        });
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }

    public ActionResult CheckTopicExists(MyViewModel model)
    { 
        return Json(false, JsonRequestBehavior.AllowGet); 
    }
}

View:

@model MyViewModel

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

@using (Html.BeginForm())
{
    @Html.EditorFor(x => x.ID1)
    @Html.EditorFor(x => x.ID2)

    @Html.LabelFor(x => x.Topic)
    @Html.EditorFor(x => x.Topic)
    @Html.ValidationMessageFor(x => x.Topic)
    <input type="submit" value="OK" />
}
like image 185
Darin Dimitrov Avatar answered Mar 20 '23 10:03

Darin Dimitrov