【问题标题】:How to access model in jqueryjquery如何访问模型
【发布时间】:2010-06-21 08:57:55
【问题描述】:

这里的问题很简单

这是我的看法

<%@ Control Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<GetmoreRevamp.BAL.Product>" %>
<link href="<%=Url.Content("~/Content/AddToCart.css")%>" rel="stylesheet"
      type="text/css" />
<link href="<%=Url.Content("~/Scripts/jquery-1.4.1.js")%>" type="text/javascript" />

<script type="text/javascript">
    function submitForm(formData) {
        var tdata = $(formData).serialize();
        $.ajax({
            type: "POST",
            url: '<%= Url.Action("AddToCart","Cart")%>',
            data: tdata,
            contentType: 'application/json; charset=utf-8',
            datatype: "json",
            success: function(result) { success(result); }
        });
        return false;
    }
    function success(result) {
        alert("success:" + result.success);
    }  
</script>

<% using (Html.BeginForm("AddToCart", "Cart ", Model, FormMethod.Post,
             new { onsubmit = "return submitForm('this');" })) {%>
<div class="prishosbtn">
    <a rel="prettyPhoto" href="" id="buy">
        <%Response.Write("<input type=\"image\" class=\"imgClass\" alt=\"" +
                         (Model != null && Model.ProductName != null ?
                                       Model.ProductName : "KOEB") + "\" src=\"" +
                        Url.Content("~/pics/undersider/listevisning/kob-knap.png") +
                          "\" id=\"ImageButton\" name=\"ImageButton\" />");%>
    </a>
</div>
<%}%>

这是我的控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GetmoreRevamp.WEB.Models;
using GetmoreRevamp.WEB.Models.BLLModels;
using System.Web.Security;
using System.Security.Principal;
using GetmoreRevamp.WEB.Utilities;
using GetmoreRevamp.BAL;

namespace GetmoreRevamp.WEB.Controllers
{
    public class CartController : Controller
    {
        //
        // GET: /Cart/
        public ActionResult Index()
        {
            return View("Cart");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddToCart(Product product)
        {
            JsonResult result = new JsonResult();
            OrderHeader orderHeader = 
                Session[Constants.CurrentlySessionOrderHeader] as OrderHeader;
            if (orderHeader == null)
            {
                orderHeader = new OrderHeader();
            }
            if (product != null && product.ProductGuid != null &&
                string.Equals(product.ProductGuid, string.Empty))
            {
                orderHeader.AddOrderLineItem(1, product);
                orderHeader.Calculate();
                Session[Constants.CurrentlySessionOrderHeader] = orderHeader;
                //Default redirection Must be changed when actual view is created
                result.Data = true;
            }
            else
            {
                //Default redirection Must be changed when actual view is created
                result.Data = false;
            }
            return result;
        }
    }
}

“产品”在 bal 中定义。产品包含其他业务实体。我只想做的是访问在 jquery 中绑定视图的模型,然后将其发布到购物车控制器中的操作方法。我不想发布产品的 ID。我想通过 jquery 将实际模型发布到我的操作方法中。我是这方面的新手。所以任何帮助和最简单的解决方案都将是首选

【问题讨论】:

    标签: asp.net asp.net-mvc forms jquery model


    【解决方案1】:

    MVC 将字段名称与 action 方法中的业务对象匹配,因此如果 Product 有 ProductID 字段,则应该有:

    Html.TextBox("ProductID")
    

    声明,或者使用 MVC 2 中的 TextBoxFor 方法。我很确定即使在使用 JQuery 发布时它仍然是这样工作的。模型绑定器处理获取表单字段并将它们传递给产品对象的过程。但是,所有字段都必须在您发布到服务器的表单中,或者您必须在传递 tdata 变量的位置显式传递参数...

    HTH。

    【讨论】:

    • Brian 我的问题是,在 jquery 中我怎样才能获得与视图绑定的实际模型对象。假设有一个模型类为 b 的强类型视图 a。现在我们可以通过 a 中的 html 元素显示或编辑 b 实例的字段。这是直截了当的。如果控制器 c 的操作方法 d 接受 b 作为参数,我们可以回发/重定向/获取到该操作方法。这很容易。我的场景是它的扩展。由于 a 将 b 的实例作为参数提交给 d,这意味着它要么已经在其中存储了 b 的实例,要么知道如何创建 b 的实例(续)
    • (接上一页)从它可用的信息。我想要做的是在 jquery 中访问该 b 的实例,并在必要时将其序列化,然后通过 json 将其提交给任何接受 b 实例作为参数的控制器中的操作方法。
    • 我想说的是,模型绑定过程在使用任何方法时都不会发布对象。它将对象的字段发布到服务器。在服务器上,模型绑定过程使用反射将名称为 ProductID 的文本框与模型引用中存储的对象属性的产品 ID 匹配。 MVC 实际上具有灵活的绑定架构,因为它也可以处理嵌套对象。您始终可以接受 FormCollection 作为操作方法参数,然后使用 TryUpdateModel 将值推送到对象。
    • 因此 MVC 不像在 Web 表单中使用 Web 服务代理生成的那样使用服务器端对象的客户端组件,它只是利用灵活的绑定架构将字段与属性名称匹配。
    猜你喜欢
    • 2015-08-04
    • 2019-03-26
    • 2011-02-18
    • 2017-11-11
    • 2012-06-14
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    相关资源
    最近更新 更多