如何在Mvc控制器的对话框中获取值?

时间:2009-07-31 03:37:28

标签: asp.net-mvc

我的对话框是一个jQuery对话框,我的弹出框使用<%Html.RenderPartial("MyUC")%>为对话框的标记调用用户控件。如何让控制器调用对话框中的值?


这是我的对话框代码

    $(function() {
        $("#dialog").dialog({
            bgiframe: true,
            autoOpen: false,
            height: 300,
            modal: true,
            buttons: {
                "Save": function() {
                    $("#edit").submit();
                    $('#dialog p').empty();
                }, 
                Cancel: function() {
                    $(this).dialog('close');
                    $('#dialog p').empty();                        
                }
            },                
            close: function() {
                allFields.val('').removeClass('ui-state-error');                   
            }
        });

        submitHandler: function(form) {
            $('#dialog p').append('Click \'OK\' to confirm Edit of <b>$' + $("#Item").val());
            $('#dialog').dialog('open');
        }

        $("input[name=Edit]").click(function() {

            var hd = $(this).next(); //will give u hidden div

            $("#dialog input[id=ItemId]").val(hd.children("#ItemId").val());
            //$("#dialog input[id=CatId]").val(hd.children("#CatId").val());
            $("#dialog select > option[id=" + hd.children("#CatId").val() + "]").attr("selected", "selected");
            $("#dialog input[id=UnitId]").val(hd.children("#UnitId").val());
            $("#dialog input[id=SaleOffId]").val(hd.children("#SaleOffId").val());
            $("#dialog input[id=ItemCode]").val(hd.children("#ItemCode").val());
            $("#dialog input[id=ItemName]").val(hd.children("#ItemName").val());
            $("#dialog input[id=UnitCost]").val(hd.children("#UnitCost").val());
            $("#dialog input[id=QuantityRemaining]").val(hd.children("#QuantityRemaining").val());
            $("#dialog form").attr("post", "/Item/EditTest/" + hd.children("#ItemId").val(),'json');

            alert("/Item/EditTest/" + hd.children("#ItemId").val());
            $('#dialog').dialog('open');
        })
    .hover(
        function() {
            $(this).addClass("ui-state-hover");
        },
        function() {
            $(this).removeClass("ui-state-hover");
        }
    ).mousedown(function() {
        $(this).addClass("ui-state-active");
    })
    .mouseup(function() {
        $(this).removeClass("ui-state-active");
    });
    });
     

这是div包含对话框

  <% Html.BeginForm("EditTest", "Item"); %>
           <table>
             <tr>
               <td><b>ItemId</b></td>
               <td><input id="ItemId" name="ItemId" type="text" disabled="disabled" /></td>
             </tr>
               <tr>
               <td><b>CatId</b></td>
               <td><input id="CatId" name="CatId" type="text" />
                   <%--<%= Html.DropDownList("CatId", ViewData["AllCategory"] as SelectList)%>--%></td>
             </tr>
               <tr>
               <td><b>SaleOffId</b></td>
               <td><input id="SaleOffId" name="SaleOffId" type="text"/></td>
             </tr>
               <tr>
               <td><b>UnitId</b></td>
               <td><input id="UnitId" name="UnitId" type="text" /></td>
             </tr>
               <tr>
               <td><b>ItemCode</b></td>
               <td><input id="ItemCode" name="ItemCode" type="text" /></td>
             </tr>
               <tr>
               <td><b>ItemName</b></td>
               <td><input id="ItemName" name="ItemName" type="text" /></td>
             </tr>
               <tr>
               <td><b>UnitCost</b></td>
               <td><input id="UnitCost" name="UnitCost" type="text"/></td>
             </tr>
               <tr>
               <td><b>QuantityRemaining</b></td>
               <td><input id="QuantityRemaining" name="QuantityRemaining" type="text"/></td>
             </tr>
             <tr>
               <td><input type="submit" id="Save" name="Save" value="Save" /></td>
               <td><input type="submit" id="Cancel" name="Cancel" value="Cancle" onclick="back(-1);" /></td>
             </tr>
           </table>

3 个答案:

答案 0 :(得分:0)

例如,如果您在TextBox中有一个vaule,则有两种方法可以从页面返回valuse。

在Controller中专门请求值。

[AcceptVerbs(HttpVerbs.Post)]
public SomeViewController(string stringValueFromTextBoxOnPartialView){}

另一种方法是将整个表单作为FormCollection请求。这是你可以添加一个fiew输入类型到你的页面,你只需要一个对象在你的控制器。

[AcceptVerbs(HttpVerbs.Post)]
public SomeViewController(FormCollection form){}

更详细的说明可以在http://www.asp.net/learn/mvc/tutorial-33-cs.aspx

找到

答案 1 :(得分:0)

您的对话框必须包含元素,哪个操作URL指向YorController / YourAction。把一些名字属性放在里面。然后在按下“Ok”按钮时提交表单 - 您将在控制器中传递值。

答案 2 :(得分:0)

如果你的对话框有一个表格字段,一开始就没有,即jQuery成功,你可以让你的控制器像这样要求它

this.Request["jQueryDialogElement"]

this.Url.RequestContext.HttpContext.Request["jQueryDialogElement"]

如果你不知道,你可以放入一个断点并查看它是否在QueryString或Request对象的Form成员中。