【问题标题】:Get changed item from DropDownListFor into another form从 DropDownListFor 获取更改的项目到另一个表单
【发布时间】:2015-02-23 15:32:36
【问题描述】:

我有 3 个表单,每个表单都可以在服务器端访问自己的删除/创建/编辑操作。

  • 当我更改 DropDownListFor 所选项目并执行 Delete 时,字符串标题将传递给服务器

  • 当我更改 DropDownListFor 所选项目并执行 Create/Edit 时,字符串标题传递给服务器。

    李>

如何让我的创建/编辑表单知道我在 DropDownListFor 中的更改?

传递初始标题值适用于创建/编辑操作。所以问题是变化事件。

index.cshtml

@using (Html.BeginForm(MVC.Configuration.Addresses.ActionNames.Delete, MVC.Configuration.Addresses.Name, new { @area = MVC.Configuration.Name }, FormMethod.Post, HtmlAttributes.Form))
        {

            <div class="form-group required">
                @Html.LabelFor(m => m.Title, HtmlAttributes.Label)
                <div class="col-md-6">
                    @(Html.Kendo().DropDownListFor(m => m.Title)
                                  .BindTo(Model.Addresses.OrderBy(order => order.Text))
                                  .HtmlAttributes(HtmlAttributes.KendoControl))
                </div>
            </div>

            <input type="submit" value="Delete" class="btn btn-default" />
        }

        @using (Html.BeginForm(MVC.Configuration.Addresses.ActionNames.Edit, MVC.Configuration.Addresses.Name, new { @area = MVC.Configuration.Name }, FormMethod.Get, HtmlAttributes.Form))
        {
            @Html.HiddenFor(p => p.Title)
            <input type="submit" value="Edit" class="btn btn-default" />
        }
        @using (Html.BeginForm(MVC.Configuration.Addresses.ActionNames.Add, MVC.Configuration.Addresses.Name, new { @area = MVC.Configuration.Name }, FormMethod.Get, HtmlAttributes.Form))
        {
            @Html.HiddenFor(p => p.Title)
            <input type="submit" value="Add" class="btn btn-default" />
        }

【问题讨论】:

  • 选择创建和编辑时,标题变量中设置了什么?
  • 您在第 2 和第 3 表单中没有下拉菜单。更不用说你生成无效的html了。

标签: asp.net-mvc kendo-ui asp.net-mvc-5 kendo-asp.net-mvc


【解决方案1】:

您可以使用 1 个表单和多个按钮,您将获得所需的结果,并且您的视图会更清晰。

据我所知主要有两种方法:

  • 1 MVC 操作:您可以检查 MVC 操作中单击的提交按钮的值,然后执行所需的操作。假设您的按钮名称为“submitButtonName”。

[HttpPost] public ActionResult YourAction(string submitButtonName, YourFormModel model) { switch (submitButtonName) { case "create": CreateMethod(model); break; case "edit": EditMethod(model); break; case "delete": DeleteMethod(model); break; } }

  • 3 MVC 操作:您可以使用 javascript 更改按钮单击时的表单目标操作。假设您的按钮具有“submitButtonClass”类的示例。

    $(".submitButtonClass").on("click", function(e){ e.preventDefault(); $('#yourFormId').attr('action', $(this).val()).submit(); });

我没有测试就快速编写了代码,但它应该可以工作:)

祝你有美好的一天,

阿尔伯托

【讨论】:

  • 我知道...但是用一种形式你如何声明 3 种不同的 mvc 动作?
  • ONE mvc 操作将不起作用或只有缺点: 1.) 我在 url 中没有编辑/添加,但 YourAction... 用户可能会问它是什么?编辑或添加... 2.) 当 switch/case 中没有 case 被命中时,我需要 YourAction 的 return 语句,否则我无法编译。
  • 我将尝试使用我现有的 3 个操作并抓住点击,但我会这样做 $('#yourFormId').attr('action',$(this).attr('name '))。提交();因为我的按钮是本地化的。
  • 忘记最后的attr名称评论值就好了!
  • @Pascal 我发现你的第二条评论有点奇怪。 1) 作为用户,我从来没有看到按钮的代码:我只是阅读文本。 2)即使您有 3 种不同的操作,您仍然需要退货。
猜你喜欢
  • 1970-01-01
  • 2013-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-15
  • 1970-01-01
相关资源
最近更新 更多