asp.net mvc 3远程属性传递3个字段

Avi*_*ash 11 attributes asp.net-mvc-3

我想使用RemoteAttribute将三个字段传递给我的控制器.我该怎么做?

 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);
        }
Run Code Online (Sandbox Code Playgroud)

我怎样才能将三个字段传递给该函数?

Dar*_*rov 35

你可以用逗号分隔它们:

AdditionalFields = "ID1, ID2"
Run Code Online (Sandbox Code Playgroud)

完整示例:

模型:

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; }
}
Run Code Online (Sandbox Code Playgroud)

控制器:

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); 
    }
}
Run Code Online (Sandbox Code Playgroud)

视图:

@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" />
}
Run Code Online (Sandbox Code Playgroud)

  • @Avinash,很好,所以也许你可以将这篇文章标记为答案,如果它对你有帮助的话? (2认同)