【问题标题】:multiple instances of a strongly typed entity in a view MVC3视图MVC3中强类型实体的多个实例
【发布时间】:2013-02-17 13:07:36
【问题描述】:

我对 MVC 很陌生,所以这可能是一个非常新手的问题。我正在玩产品目录。我有一个要以表格格式(表格或 div)显示的产品列表。每行将显示一个产品信息,并有两个链接“编辑和删除”。我想使用 Jquery Ajax 向我的控制器提交请求。对于编辑,我想显示一个模式对话框。为了删除,我想从目录中删除产品。每一项操作都将在完成后更新网格。


这是一些伪代码:

型号:

 public class ProductModel
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public int Id { get; set; }
        public bool IsReadOnly { get; set; }
    }

控制器:

public class ProductController : Controller
    {
        //
        // GET: /Product/

        public ActionResult Edit(ProductModel product)
        {

        }

        public ActionResult Delete(ProductModel product)
        {

        }


    }

编辑视图:

@model Generic.Controller.Models.ProductModel

if Model.IsReadOnly
    Render display editor
else
    Render editor

列表视图:

@model Generic.Controller.Models.ProductModel

@using (Html.BeginForm())
{
    //for each model item
    <div class="some-css">
        <div class="column-div">
            //display the model
        </div>

        <div class="column-div">
            //display the edit link   
        </div>

        <div class="column-div">
            //display the delete link   
        </div>
    </div>
}

替代列表视图 - 从列表视图呈现的部分视图

//对于每个模型:

@using (Html.BeginForm())  //id this form with the product id?
{
    //for each model item
    <div class="some-css">
        <div class="column-div">
            //display the model
        </div>

        <div class="column-div">
            //display the edit link   
        </div>

        <div class="column-div">
            //display the delete link   
        </div>
    </div>
}

这里有一些 Javascript 框架:

var open = function (methodName, url) {        
    $("#dialogDiv").dialog({
       buttons: {
            Save: function () {
                var form = $(options.formToPost);
                $.ajax({
                    type: "POST",

这里发生了什么?通常我可以:

               url: form.attr('action'),
               data: form.serialize()  

但这是否意味着每个实体都有一个表单?

success: function (data, status, xhr) {
                    if (data.IsValid) {
                        //how do I identify the div I need to update?
                    } else {

                    }
                }
            });
        },

        Cancel: function () {
            $("#dialogDiv").dialog('close');
            $("#dialogDiv").empty();
        }
    }
});
$.ajax(
    {
        Type: methodName,
        url: url,
        success: function (data, status, xhr) {
            openDialog(data);
        }
    });

function openDialog(data) {
    $("#dialogDiv").html(data);
    $("#dialogDiv").dialog('open');
}
};

return {
    open: open
};

这是我的问题:

  1. 现在我唯一知道如何轻松发布的就是表单。为了向我的控制器发布 ajax 请求,我是否必须有多个表单(每个产品 1 个)?
  2. 如果我使用 JQuery 找到与单击的链接最近的 div 并将其发送到控制器,模型绑定器会自动将 div 的内容绑定到我的 ProductModel 吗?
  3. 在编辑或删除产品后更新视图的最佳方式是什么?

谢谢,

【问题讨论】:

  • 可以不使用表单发送数据到服务器,看jQuery API中的例子

标签: ajax asp.net-mvc asp.net-mvc-3 jquery


【解决方案1】:
  • 使用 [HttpPost] 装饰您的控制器操作以进行编辑和删除,以防止 GET 请求编辑/删除。
  • 如果您使用模态框编辑行,则每个呈现的模态框都需要是一个表单,以便将值发布到控制器操作。
  • 每次编辑或删除后执行redirectToAction("NameOfListMethod") 并更新列表。

如果你想用 jQuery 处理所有事情,那就有点棘手了,因为你需要能够在客户端更新表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多