ASP.NET MVC 3的jquery ajax表单

时间:2022-12-04 17:25:15

this might be an easy question but for me right now it is not clear and I have to get things sorted in my head... maybe somebody can help me with that =)...

这可能是一个简单的问题,但对我来说现在还不清楚,我必须把事情分类在我脑海里......也许有人可以帮助我=)...

I know that MVC comes with Ajax Helpers. I know there is Microsoft library and there is also jquery. I know that with MVC 3 they introduced unobtrusive javascript which adds some special tags to html for cleannes...

我知道MVC附带了Ajax Helpers。我知道有Microsoft库,还有jquery。我知道在MVC 3中他们引入了不引人注目的javascript,它为html添加了一些特殊的标签,用于清洁......

but how does ist play together?

但是怎么一起玩?

Example: I want to post a remote form (partial view) per ajax to add comments to a blog post. without posting the whole page back.

示例:我想根据ajax发布远程表单(部分视图)以向博客帖子添加评论。没有发回整个页面。

in my Partial View would I use Ajax.BeginForm() is this then MvcAjax or Jquery? Or would I use Html.BeginForm() and register something like $.post on the click event of the Form. This would also have a fallback of plain html if javascript is disabled or not supported....

在我的Partial View中我会使用Ajax.BeginForm()是MvcAjax还是Jquery?或者我会使用Html.BeginForm()并在Form的click事件上注册类似$ .post的内容。如果javascript被禁用或不受支持,这也会有普通html的后备....

Or in general when to use what for posting comments to a blog post?... and I assume that it is correct, that I am posting to the create action of the commentscontroller and I would use the JsonModelBinder to transform it to a model. After that I would return Json and would append it to my comments list...

或者一般来说什么时候使用什么发布评论到博客文章?...我认为它是正确的,我发布到commentscontroller的创建动作,我会使用JsonModelBinder将其转换为模型。之后我会返回Json并将其附加到我的评论列表中......

Is this reasonable why of doing it?

这样做是否合理?

1 个解决方案

#1


30  

Ajax.BeginForm() is this then MvcAjax or Jquery?

Ajax.BeginForm()是MvcAjax还是Jquery?

By default it is jquery. You need to reference the jquery.unobtrusive-ajax.js script for this to work.

默认情况下它是jquery。您需要引用jquery.unobtrusive-ajax.js脚本才能使其正常工作。

Or would I use Html.BeginForm() and register something like $.post on the click event of the Form.

或者我会使用Html.BeginForm()并在Form的click事件上注册类似$ .post的内容。

That's an alternative. Personally that's what I do.

那是另一种选择。就个人而言,这就是我的工作。

I assume that it is correct, that I am posting to the create action of the commentscontroller and I would use the JsonModelBinder to transform it to a model. After that I would return Json and would append it to my comments list...

我假设它是正确的,我发布到commentscontroller的创建操作,我将使用JsonModelBinder将其转换为模型。之后我会返回Json并将其附加到我的评论列表中......

The JsonModelBinder has been introduced in ASP.NET MVC 3 and it allows you to send a JSON string to a controller action which will be mapped back to a view model. For example if you have the following view model:

JsonModelBinder已经在ASP.NET MVC 3中引入,它允许您将JSON字符串发送到控制器操作,该控制器操作将映射回视图模型。例如,如果您有以下视图模型:

public class PersonViewModel
{
    public string Name { get; set; }
    public int Age { get; set; }
}

and the following action:

并执行以下操作:

public ActionResult Foo(PersonViewModel person) 
{
    ...    
}

the traditional way to invoke it in AJAX is:

在AJAX中调用它的传统方法是:

$.ajax({
    url: '@Url.Action("foo")',
    type: 'POST',
    data: { name: 'john', age: 20 },
    success: function(result) {
        // TODO:
    }
});

and in ASP.NET MVC 3 you could send a JSON as request parameter which will be bound to the PersonViewModel action parameter:

在ASP.NET MVC 3中,您可以发送一个JSON作为请求参数,该参数将绑定到PersonViewModel操作参数:

$.ajax({
    url: '@Url.Action("foo")',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify({ name: 'john', age: 20 }),
    success: function(result) {
        // TODO:
    }
});

#1


30  

Ajax.BeginForm() is this then MvcAjax or Jquery?

Ajax.BeginForm()是MvcAjax还是Jquery?

By default it is jquery. You need to reference the jquery.unobtrusive-ajax.js script for this to work.

默认情况下它是jquery。您需要引用jquery.unobtrusive-ajax.js脚本才能使其正常工作。

Or would I use Html.BeginForm() and register something like $.post on the click event of the Form.

或者我会使用Html.BeginForm()并在Form的click事件上注册类似$ .post的内容。

That's an alternative. Personally that's what I do.

那是另一种选择。就个人而言,这就是我的工作。

I assume that it is correct, that I am posting to the create action of the commentscontroller and I would use the JsonModelBinder to transform it to a model. After that I would return Json and would append it to my comments list...

我假设它是正确的,我发布到commentscontroller的创建操作,我将使用JsonModelBinder将其转换为模型。之后我会返回Json并将其附加到我的评论列表中......

The JsonModelBinder has been introduced in ASP.NET MVC 3 and it allows you to send a JSON string to a controller action which will be mapped back to a view model. For example if you have the following view model:

JsonModelBinder已经在ASP.NET MVC 3中引入,它允许您将JSON字符串发送到控制器操作,该控制器操作将映射回视图模型。例如,如果您有以下视图模型:

public class PersonViewModel
{
    public string Name { get; set; }
    public int Age { get; set; }
}

and the following action:

并执行以下操作:

public ActionResult Foo(PersonViewModel person) 
{
    ...    
}

the traditional way to invoke it in AJAX is:

在AJAX中调用它的传统方法是:

$.ajax({
    url: '@Url.Action("foo")',
    type: 'POST',
    data: { name: 'john', age: 20 },
    success: function(result) {
        // TODO:
    }
});

and in ASP.NET MVC 3 you could send a JSON as request parameter which will be bound to the PersonViewModel action parameter:

在ASP.NET MVC 3中,您可以发送一个JSON作为请求参数,该参数将绑定到PersonViewModel操作参数:

$.ajax({
    url: '@Url.Action("foo")',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify({ name: 'john', age: 20 }),
    success: function(result) {
        // TODO:
    }
});