如何追加整套模式在MVC FORMDATA和获得它 [英] how to append whole set of model to formdata and obtain it in MVC

查看:154
本文介绍了如何追加整套模式在MVC FORMDATA和获得它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过FORMDATA通过一整套模型对象,并将其转换为   在控制器的型号。

how to pass whole set model object through formdata and convert it to model type in controller.

下面是如何,我已经试过到目前为止!

Below is how I've tried till now!

我的js部分:

model = {
             EventFromDate: fromDate,
             EventToDate: toDate,
             ImageUrl: imgUrl,
             HotNewsDesc: $("#txthtDescription").val().trim(),
        };
formdata.append("model",model);

然后通过它通过AJAX这也将是字符串,如果我检查的Request.Form值[模型] 的结果是一样的,我的意思是将收到的字符串值,将[对象的对象]

then pass it through ajax it will also be string and if I check the value of Request.Form["model"] the result will be same, I mean it will be received as string and value will be "[object object]"

有没有办法通过FORMDATA传递模型,并接受它的控制?

推荐答案

如果你的观点是基于模型和已生成的内部&LT的控制;形式> 标签,那么你可以使用序列化模型 FORMDATA

If your view is based on a model and you have generated the controls inside <form> tags, then you can serialize the model to FormData using

var formdata = new FormData($('form').get(0));

这也将包括与生成的任何文件&LT;输入类型=文件名称=MYIMAGE... /&GT;

This will also include any files generated with <input type="file" name="myImage" .../>

和使用后回来

$.ajax({
  url: '@Url.Action("YourActionName", "YourControllerName")',
  type: 'POST',
  data: formdata,
  processData: false,
  contentType: false,         
});

和在控制器

[HttpPost]
public ActionResult YourActionName(YourModelType model)
{
}

或者(如果你的模型中不包括的属性 HttpPostedFileBase

[HttpPost]
public ActionResult YourActionName(YourModelType model, HttpPostedFileBase myImage)
{
}

如果你想补充一点,是不是在形式的附加信息,那么你就可以使用其追加

If you want to add additional information that is not in the form, then you can append it using

formdata.append('someProperty', 'SomeValue');

这篇关于如何追加整套模式在MVC FORMDATA和获得它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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