0

我正在寻找如何使用表单创建模式对话框;我已经在asp.net mvc上有表格,是一个页面;我想要,表单在模态对话框中收费。

任何人都知道该怎么做,或者我在哪里可以找到一些信息,因为我发现的一切都是在创建一个新表单,但我不知道如何做我需要的

  dialog.load(
  $("#dialog").dialog({
      close: function(event, ui) {
          dialog.remove();
      },
      resizable: false,
      height: 140,
      width: 460
      modal: true,
      buttons: {
          "Ok": function() {
              $(this).dialog("close");
              isConfirmed = true;
              $("form").submit();
           },
           Cancel: function() {
              $(this).dialog("close");
           }
       }

我曾经使用过这样的东西,但我知道如何更改以使用表单来整理我的页面,或者如何做到这一点

4

1 回答 1

1

第一步是将此表单放入局部视图中,并让控制器操作为该局部视图提供服务。因此,让我们以一个示例控制器为例:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult MyForm()
    {
        var model = new MyViewModel();
        return PartialView(model);
    }

    [HttpPost]
    public ActionResult MyForm(MyViewModel model)
    {
        if (!ModelState.IsValid)
        {
            // there were validation errors => redisplay
            // the form so that the user can fix them
            return PartialView(model);
        }

        // at this stage validation has passed => we could do
        // some processing and return a JSON object to the client
        // indicating the success of the operation
        return Json(new { success = true });
    }
}

MyForm 动作用于分别显示表单并在提交时对其进行处理。Index 操作将简单地提供一个包含允许弹出模式的链接的视图。

所以这里是MyForm.cshtml部分:

@model MyViewModel

@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "dialog", OnSuccess = "submitSuccess" }))
{
    @Html.LabelFor(x => x.Foo)
    @Html.EditorFor(x => x.Foo)
    @Html.ValidationMessageFor(x => x.Foo)
    <button type="submit">OK</button>
}

最后是Index.cshtml视图:

<script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        // we subscribe to the click handler of the link
        $('#mylink').click(function () {
            // when the anchor is clicked we attach the dialog to the div
            var url = this.href;
            $('#dialog').dialog({
                open: function (event, ui) {
                    // when the dialog is shown we trigger an AJAX request
                    // to the MyForm action to retrieve the contents of the 
                    // form and show it
                    $(this).load(url);
                }
            });
            return false;
        });
    });

    // this function is used by the Ajax form. It is called
    // when the form is submitted.     
    var submitSuccess = function (result) {
        // we check to see if the controller action that was 
        // supposed to process the submission of the form
        // returned a JSON object indicating the success of the
        // operation
        if (result.success) {
            // if that is the case we thank the user and close the dialog
            alert('thanks for submitting');
            $('#dialog').dialog('close');
        }
    };
</script>

@Html.ActionLink("Show modal form", "myform", null, new { id = "mylink" })
<div id="dialog"></div>

显然,应该将 javascript 外部化到一个单独的文件中,但出于演示的目的,我将其保留在视图中。

于 2012-04-17T06:18:32.510 回答