ASP.NET MVC 2 使用 jQuery 加载部分视图 - 无客户端验证
我正在使用 jQuery.load() 来渲染部分视图。这部分看起来像这样:
$('#sizeAddHolder').load(
'/MyController/MyAction', function () { ... });
我的控制器中的操作代码如下:
public ActionResult MyAction(byte id)
{
var model = new MyModel
{
ObjectProp1 = "Some text"
};
return View(model);
}
[HttpPost]
public ActionResult MyAction(byte id, FormCollection form)
{
// TODO: DB insert logic goes here
var result = ...;
return Json(result);
}
我返回一个看起来像这样的部分视图:
<% using (Html.BeginForm("MyAction", "MyController")) {%>
<%= Html.ValidationSummary(true) %>
<h3>Create my object</h3>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%= Html.LabelFor(model => model.ObjectProp1) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Size.ObjectProp1) %>
<%= Html.ValidationMessageFor(model => model.ObjectProp1) %>
</div>
div class="editor-label">
<%= Html.LabelFor(model => model.ObjectProp2) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.ObjectProp2) %>
<%= Html.ValidationMessageFor(model => model.ObjectProp2) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
客户端验证在这种情况下不起作用。此外,包含验证消息的脚本也不包含在返回的视图中。我的模型类中的两个属性都有 Required
和 StringLength
属性。 有什么方法可以在这样加载的视图中触发客户端验证吗?
I am using jQuery.load()
to render a partial view. This part looks like this:
$('#sizeAddHolder').load(
'/MyController/MyAction', function () { ... });
The code for actions in my controller is the following:
public ActionResult MyAction(byte id)
{
var model = new MyModel
{
ObjectProp1 = "Some text"
};
return View(model);
}
[HttpPost]
public ActionResult MyAction(byte id, FormCollection form)
{
// TODO: DB insert logic goes here
var result = ...;
return Json(result);
}
I am returning a partial view that looks something like this:
<% using (Html.BeginForm("MyAction", "MyController")) {%>
<%= Html.ValidationSummary(true) %>
<h3>Create my object</h3>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%= Html.LabelFor(model => model.ObjectProp1) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Size.ObjectProp1) %>
<%= Html.ValidationMessageFor(model => model.ObjectProp1) %>
</div>
div class="editor-label">
<%= Html.LabelFor(model => model.ObjectProp2) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.ObjectProp2) %>
<%= Html.ValidationMessageFor(model => model.ObjectProp2) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
Client side validation does not work in this case. What is more the script that contains validation messages also isn't included in the view that's returned. Both properties in my model class have Required
and StringLength
attributes.
Is there any way to trigger client side validation in a view which has been loaded like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
首先,您应该返回部分视图而不是视图。
其次,您是否尝试使用 AJAX 加载此部分视图?在这种情况下,您可能需要使用 jquery.ajax
First of all you should return a partial view and not a view.
Second, are you trying to load this partial view with AJAX? In that case you might want to use jquery.ajax
您应该在 ajax 调用中使用 dataType
来自 jQuery 文档:
dataType
默认值:智能猜测(xml、json、脚本或 html)
您期望从服务器返回的数据类型。如果未指定,jQuery 将根据响应的 MIME 类型智能地尝试获取结果(XML MIME 类型将生成 XML,在 1.4 JSON 中将生成 JavaScript 对象,在 1.4 脚本中将执行脚本,以及任何其他内容)否则将以字符串形式返回)。可用的类型(以及作为成功回调的第一个参数传递的结果)是:
You should use dataType on the ajax call
From jQuery docs:
dataType
Default: Intelligent Guess (xml, json, script, or html)
The type of data that you're expecting back from the server. If none is specified, jQuery will intelligently try to get the results, based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:
这是适用的: asp-net-mvc-client-side-validation-with-dynamic-contents
this is applicable: asp-net-mvc-client-side-validation-with-dynamic-contents
问题是使用 ajax 加载的表单永远不会注册到 Microsoft 验证。要解决此问题,请调用以下函数 Sys.Mvc.FormContext._Application_Load。
那应该解决它。另外,请确保通过 ajax 加载的表单具有唯一的 ID。否则,验证将失败。
避免使用 load()。它会删除响应中加载的所有脚本。
The problem is that the form loaded with ajax never gets registered with the Microsoft validation. To solve it call the following function Sys.Mvc.FormContext._Application_Load.
That should fix it. Also, make sure that the forms you load via ajax have unique id's. Otherwise, the validation will fail.
Avoid using load(). It removes any scripts loaded in the response.
我不确定您是否仍在寻找问题的答案,但我已经写了一篇关于使 AJAX 加载的表单与 ASP.NET MVC 2 中的客户端验证一起使用的文章:
http://tpeczek.com/2010/04/制作-aspnet-mvc-2-client-side.html
I'm not sure if you are still loking for answer to your question, but I have write a post about making AJAX loaded forms work with client-side validation in ASP.NET MVC 2:
http://tpeczek.com/2010/04/making-aspnet-mvc-2-client-side.html
在 html 文件中,您可以包含以下内容:
对此表示抱歉,但我不知道如何发布链接 html。因此,您可以有一个链接 class="delete"、id= value 和 href = "javascript: ;”
然后我使用这个函数来渲染部分视图:
记住在控制器中你必须有一个类似的操作:
In the html file you can have something like:
sorry about that but I don't no how post a link html. So you can have a link class="delete", id= value and a href = "javascript:;"
Then I used this function to render a partial view:
Remember in the controler you must have a action like: