如何从视图通过jQuery通过全模型控制器在MVC C#应用程序 [英] How to pass the full Model from view to controller via jquery in a MVC c# application

查看:117
本文介绍了如何从视图通过jQuery通过全模型控制器在MVC C#应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有复选框的和TextBox的列表。我希望让用户通过一个局部视图模式弹出项目添加到列表中。

I have a list of checkbox's and textbox's. I want to let the user add items to the list via a partial view modal popup.

在用户添加一个项目到列表中,如果在原始视图的任何项目在他们的价值观,我希望他们preserved,页面刷新与添加的项目。

After the user adds an item to the list, if any items on the original view have values in them, I want them preserved, and the page refreshed with the added items.

我要的是整个模型发送回从原始视图控制器,然后我可以只添加新项目的模型,并通过模型回到原来的页面,并拥有所有我的价值preserved。

I want to send the full model back to the controller from the original view, I can then just add the new items to that model and pass the model back to the original page and have all my values preserved.

我可以抓住所有的值,并通过在JavaScript(非常乏味)的循环和传递等他们,但我觉得整个模型是最简单的方法。

I could grab all the values and pass them via loops and such in javascript (very tedious), but I think the full model would be the easiest way.

我sawe从Laviak一个职位从<一个链接href=\"http://stackoverflow.com/questions/1518417/how-to-send-a-model-in-jquery-ajax-post-request-asp-net-mvc\">here...,但我不能得到它的工作。

I sawe a link from Laviak on a post from here..., but I can't get it to work.

它指出....
如果您需要发送完整的模型到控制器,首先需要模型来提供给您的JavaScript code。

it states.... If you need to send the FULL model to the controller, you first need the model to be available to your javascript code.

在我们的应用程序,我们这样做有一个扩展方法:

In our app, we do this with an extension method:

public static class JsonExtensions 
{ 
    public static string ToJson(this Object obj) 
    { 
       return new JavaScriptSerializer().Serialize(obj); 
   } 
} 

在看,我们用它来渲染模型:

On the view, we use it to render the model:

<script type="javascript"> 
  var model = <%= Model.ToJson() %> 
</script> 

您可以再通过模型变量到$就来电。

You can then pass the model variable into your $.ajax call.

有没有人有这个工作?

Has anyone got this to work???

谢谢
比尔

推荐答案

你可以做这样的事情:

<script type="text/javascript">
    var dataViewModel = @Html.Raw(Json.Encode(Model)); //Make sure you send the proper model to your view

    function MethodPost(param1, param2, etc...) {

    dataviewModel.Param1 = param1; //Or load a value from jQuery
    dataviewModel.Param2 = $("#param2").val();

    }

    //Pass it to a controller method
    $.post("@Url.Action(MVC.Home.PostMethodInController())", { viewModel: JSON.stringify(dataViewModel)} , function(data) {
        //Do something with the data returned.
    }
</script>

在您使用让您的类/型号控制器 Json.Net 这可在的NuGet。

In the controller you get your class/model using Json.Net which is available on nuget.

    public virtual ActionResult Index()
    {
        return View(new MyModelToView());//Send at least an empty model
    }

    [HttpPost]
    public virtual JsonResult PostMethodInController(string viewModel)
    {
        var entity = JsonConvert.DeserializeObject<MyObject>(viewModel);

        //Do Something with your entity/class

        return Json(entity, JsonRequestBehavior.AllowGet);
    }

希望这有助于。

这篇关于如何从视图通过jQuery通过全模型控制器在MVC C#应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆