asp.net-mvc - 如何将 json 对象从 Javascript 传递到 asp.net mvc Controller

标签 asp.net-mvc

我刚开始 MVC 所以有很多困惑。
每当我们在 asp.net webform 项目中通过 jquery 调用任何服务器端函数时,该方法必须是静态的,并且必须由 webmethod 属性修饰。所以我想知道在 mvc 的情况下也适用相同的规则。

我得到了一个代码,但我没有测试它。

客户端方法

function getTradeContribs(id,pfid, nodedates) {

    var data = {};
    data.portfolioId = pfid;
    data.nodedates = nodedates;

    $.ajax({
            type: "POST",
            url: "/Portfolios/getTradeContribs/"+id,
            dataType: "json",
            data: JSON.stringify(data),
            contentType: "application/json; charset=utf-8",
            success: parseTradeContribs,
            error: function (error) {
                    alert("failed in opening XML file !!!");
            }
    });
   }

服务器端方法
public string getTradeContribs(int id,string portfolioId, string nodedates)
{
    string jsonTest = @" {""nodedates"": ""date"":""01/01/2012""}";
    return jsonTest;
}

从上面的代码我有几个问题
1)mvc中存在多少种 Controller 方法
2) url: "/Portfolios/getTradeContribs", 它是什么样的网址。
投资组合是 Controller 名称,getTradeContribs 是操作名称?
如果没有那么 getTradeContribs 是一种什么样的方法。

3) getTradeContribs 没有返回 ActionResult 为什么
4) ActionResult 的意义是什么?
5) 为什么 id 作为查询字符串传递,其余数据作为 json 传递。
它将如何运作?

请讨论这一点,因为我是 mvc 新手

最佳答案

whenever we call any server side function by jquery in asp.net webform project then that method has to be static and must be decorated by webmethod attribute. so i want to know that is the same rule applies in case of mvc.



一点都不。 ASP.NET MVC 是一种完全不同的模式。在 ASP.NET MVC 中,您使用返回操作结果的 Controller 操作(从 ActionResult 派生的类)。因此,如果您想返回 JSON,您只需定义一个返回 JsonResult 的 Controller 操作。并将模型传递给它:
public ActionResult GetTradeContribs(int id, string portfolioId, string nodedates)
{
    var model = new 
    {
        nodedates = "foo",
        date = DateTime.Now
    };
    return Json(model, JsonRequestBehavior.AllowGet);
}

甚至更好地定义 View 模型:
public class TradeContribsRequestViewModel
{
    public int Id { get; set; }
    public string PortfolioId { get; set; }
    public string NodeDates { get; set; }
}

public class TradeContribsViewModel
{
    public string NodeDates { get; set; }
    public DateTime Date { get; set; }
}

进而:
public ActionResult GetTradeContribs(TradeContribsRequestViewModel request)
{
    var model = new TradeContribsViewModel
    {
        NodeDates = "foo",
        Date = DateTime.Now
    };
    return Json(model, JsonRequestBehavior.AllowGet);
}

ASP.NET MVC 会自动将模型序列化为 JSON 字符串并将其传递给 View 。然后你可以使用ajax调用它:
$.ajax({
    url: '@Url.Action("GetTradeContribs", "Portfolios")',
    type: 'POST',
    data: { 
        id: 123, 
        portfolioId: 'some id', 
        nodedates: 'some node dates' 
    },
    success: function(result) {
        // You could directly use the properties of the result object here
        // like for example: alert(result.nodedates);
    }
});

您绝对不应该像在原始 getTradeContribs 中那样对 JSON 进行硬编码。方法。这是框架已经为您处理的基础设施管道。您只需要定义:
  • 返回 ActionResult 的 Controller 操作
  • Controller 操作将传递给 View 的模型类型
  • View
  • 关于asp.net-mvc - 如何将 json 对象从 Javascript 传递到 asp.net mvc Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11832948/

    相关文章:

    c# - 如何在复合模型上应用 Required 属性?

    jquery - 页面滚动到不同页面

    ASP.NET MVC 3 - 处理多个域并共享单个代码库

    asp.net-mvc - MVC razor - 从 subview 向共享布局父 div 添加类

    c# - 默认 ASP.NET MVC 上的 https

    c# - 显示总行数 Grid.MVC 列表的最佳方式

    javascript - 为什么从模型获取值时我的引导日期选择器日历不更新?

    c# - MVC 4 : How to maintain sessions and cookies to be still valid after IIS restart?

    c# - MVC3 JQuery 模态对话框在关闭和重新打开时保留内容

    jquery - "A circular reference was detected while serializing an object of type ' 系统.Reflection.RuntimeModule '"