MrD*_*pan 32 asp.net asp.net-mvc jquery json
我有一个允许用户输入/编辑新Widget数据的视图.我想将这些数据组成一个json对象并通过AJAX将它发送给我的控制器,这样我就可以在没有回发的情况下在服务器上进行验证.
我已经完成了所有工作,除了我无法弄清楚如何传递数据,所以我的控制器方法可以接受复杂的Widget类型而不是每个属性的单独参数.
所以,如果这是我的对象:
public class Widget
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我希望我的控制器方法看起来像这样:
public JsonResult Save(Widget widget)
{
...
}
Run Code Online (Sandbox Code Playgroud)
目前,我的jQuery看起来像这样:
var formData = $("#Form1").serializeArray();
$.post("/Widget/Save",
formData,
function(result){}, "json");
Run Code Online (Sandbox Code Playgroud)
我的表单(Form1)为Widget(Id,Name,Price)上的每个属性都有一个输入字段.这很好用,但它最终将Widget的每个属性作为单独的参数传递给我的控制器方法.
有没有一种方法可以"拦截"数据,可能使用ActionFilterAttribute,并在调用控制器方法之前将其反序列化为Widget对象?
MrD*_*pan 25
谢谢杰夫,让我走上了正确的道路.DefaultModelBinder非常聪明,可以为我做所有的魔术......我的问题出在我的Widget类型中.在我的仓促中,我的类型被定义为:
public class Widget
{
public int Id;
public string Name;
public decimal Price;
}
Run Code Online (Sandbox Code Playgroud)
请注意,该类型具有公共字段而不是公共属性.一旦我将它们更改为属性,它就有效了.这是正确工作的最终源代码:
Widget.aspx:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="Widget.aspx.cs" Inherits="MvcAjaxApp2.Views.Home.Widget" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<script src="../../Scripts/jquery-1.2.6.js" type="text/javascript"></script>
<script type="text/javascript">
function SaveWidget()
{
var formData = $("#Form1").serializeArray();
$.post("/Home/SaveWidget",
formData,
function(data){
alert(data.Result);
}, "json");
}
</script>
<form id="Form1">
<input type="hidden" name="widget.Id" value="1" />
<input type="text" name="widget.Name" value="my widget" />
<input type="text" name="widget.Price" value="5.43" />
<input type="button" value="Save" onclick="SaveWidget()" />
</form>
</asp:Content>
Run Code Online (Sandbox Code Playgroud)
HomeController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace MvcAjaxApp2.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Title"] = "Home Page";
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
ViewData["Title"] = "About Page";
return View();
}
public ActionResult Widget()
{
ViewData["Title"] = "Widget";
return View();
}
public JsonResult SaveWidget(Widget widget)
{
// Save the Widget
return Json(new { Result = String.Format("Saved widget: '{0}' for ${1}", widget.Name, widget.Price) });
}
}
public class Widget
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
请注意(在MrDustpan的解决方案中)MVC Action方法中的参数名称 窗口小部件必须与ASPX文件中name属性中使用的前缀匹配.
如果不是这种情况,则Action方法将始终接收null对象.
<input type="text" name="widget.Text" value="Hello" /> - OK
<input type="text" name="mywidget.Text" value="Hello" /> - FAILS
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
23350 次 |
最近记录: |