如何利用 ASP.NET MVC 中的 PartialView、AJAX 和模型验证属性?

发布于 2024-10-04 05:23:11 字数 1202 浏览 3 评论 0原文

我是 MVC 新手,正在尝试了解 AJAX 调用和模型验证。这就是我所拥有的:

一个控制器操作,它为带有单个文本框和链接的视图提供服务。视图如下所示:

<div> 
... 
<input type="text" name="registration" value=""/><a id="checkRegoLink" href="#">Check rego</> 
... 
</div>

checkRegoLink 对操作执行 $.ajax() POST 调用,该操作提供 PartialViewResult。

$.ajax({
  url:"/Entry/CheckRego",
  type: "POST",
  data: {...stuff...},
  success: function (data, status, request){
    $("#target").html(data);
  },
});

PartialViewResult 提供强类型分部视图_CreateEntry。该视图的模型具有一些用于各种属性的[Required] 属性。以下是此部分视图的一些片段:

@model namespace.EntryModel
@{ Html.EnableClientValidation(); }
@using (Html.BeginForm("CreateEntry", "Entry", FormMethod.Post))
{
 ...various entry fields, labels and stuff...
 <input type="submit" value="Add Entry"/>
}

我的问题是,如何将该部分视图返回到 $("#target") div 并包含验证错误消息。在从强类型视图接受 POST 的控制器操作中,我尝试了 return PartialView("_CreateEntry",model) - 但这会返回partialview本身,并包含验证错误消息,但是当然其本身,没有父布局。

任何想法将不胜感激。我通过投票和接受答案慢慢变得更好。请原谅我的统计数据较低 - 我一定会投票选出答案并随时接受它们。

谢谢, 丹妮.

附言。 我正在使用 MVC 3 最新的 RC

I am new to MVC and trying to get my head around AJAX calls and model validation. Here's what I have:

A Controller action that serves a View with a single textbox and a link. The View looks like:

<div> 
... 
<input type="text" name="registration" value=""/><a id="checkRegoLink" href="#">Check rego</> 
... 
</div>

The checkRegoLink does an $.ajax() POST call to an action, which serves a PartialViewResult.

$.ajax({
  url:"/Entry/CheckRego",
  type: "POST",
  data: {...stuff...},
  success: function (data, status, request){
    $("#target").html(data);
  },
});

The PartialViewResult serves a strongly-typed partial view _CreateEntry. The model for that view has some [Required] attributes for various properties. Here's some snippet of this partial view:

@model namespace.EntryModel
@{ Html.EnableClientValidation(); }
@using (Html.BeginForm("CreateEntry", "Entry", FormMethod.Post))
{
 ...various entry fields, labels and stuff...
 <input type="submit" value="Add Entry"/>
}

My question is, how do I return that partial view back into the $("#target") div complete with validation error messages. In the controller action that accepts the POST from the strongly-typed view I have tried return PartialView("_CreateEntry",model) - but that returns the partialview itself, complete with validation error messages, but of course on its own, without the parent layout.

Any ideas would be greatly appreciated. I am slowly getting better with voting up and accepting answers. Please excuse my low stats - I will definitely vote up answers and accept them as I go.

Thanks,
Dany.

PS.
I am using MVC 3 latest RC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉宸 2024-10-11 05:23:11

尝试使用 Ajax.BeginForm() 而不是 Html.BeginForm()。看看用法,它应该允许您指定不同的选项,包括要更新的 div。除非您有手动执行此操作的理由,否则为什么要手动执行已内置的操作?

编辑:

你说“我的问题是,如何将部分视图返回到 $("#target") div 并完成验证错误消息。”

然后你说“...但这会返回partialview本身,并包含验证错误消息,但当然是它自己的,没有父布局。” <-- 这不就是你想要的吗?

这不是你想要的吗?部分视图包含验证错误消息吗?

Try using Ajax.BeginForm() instead of Html.BeginForm(). Look at the usage, it should allow you to specify different options, including the div to update. Unless you have reasons for doing it manually, why manually do whats already built in?

Edit:

You say "My question is, how do I return that partial view back into the $("#target") div complete with validation error messages."

Then you say "... but that returns the partialview itself, complete with validation error messages, but of course on its own, without the parent layout." <-- isn't this exactly what you want?

Isn't that what you want? The partialview complete with validation error messages?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文