我想使用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);
}
如何将三个字段传递给该函数?
答案 0 :(得分:35)
您可以用逗号分隔它们:
AdditionalFields = "ID1, ID2"
完整示例:
型号:
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; }
}
控制器:
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);
}
}
查看:
@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" />
}
答案 1 :(得分:1)
请注意发送日期,有时控制器接收日期格式错误:是dd / mm / yyyy,接收mm / dd / yyyy
答案 2 :(得分:0)
而不是使用
public ActionResult CheckTopicExists(MyViewModel model)
如果您使用
public ActionResult CheckTopicExists(FormCollection Collection)
然后你可以重用其他类的代码