【问题标题】:button event not calling the action method in mvc按钮事件未调用 mvc 中的操作方法
【发布时间】:2014-10-15 14:23:43
【问题描述】:

在我的 mvc 应用程序中,控制器方法根本没有调用,我使用脚本调用该方法,我是新手

     $('#submit_fourth').click(function () {

    alert('data sent');
    $.ajax({
        url: "/Home/Index",
        data: { 'Id': groupId },
        type: 'GET',
        success: function (result) {
            //do the necessary updations
        },
        error: function (result) {
        }
    });
    });

按钮事件是

    <input class="send submit" type="submit" id="submit_fourth"  value="" />

我的行动是

 public ActionResult Index(Models.Companyregister comreg)
    {


        if (ModelState.IsValid)
        {


            dataaccess.Companyregister_D ds = new dataaccess.Companyregister_D();

            int a = ds.insertcompanyregister(comreg);

                        }
        return View();
    }

我的问题是如何将模型传递给控制器​​方法以及如何调用控制器方法?感谢您的支持

【问题讨论】:

  • 添加您的按钮和操作代码..
  • 完成按钮和动作
  • @dazzlingkumar - 请参阅下面的答案,请注意类型为“POST”,因为您要发布数据并且模型需要转换为纯字符串。

标签: asp.net-mvc asp.net-mvc-4


【解决方案1】:

如何将模型传递给控制器​​方法以及如何调用 控制方法?

您可以通过以下方式将数据发送到控制器操作:

Index.cshtml

<input class="send submit" type="submit" id="submit_fourth"  value="" />

<script>

    $('#submit_fourth').click(function() {

        alert('data sent');

        var model = { Name: "ABC Inc", UsedForYear: "2014" };
        $.ajax({
                url: '@Url.Action("Index", "Home")',
                contentType: 'application/json; charset=utf-8',
                type: 'POST',
                dataType: 'html',
                data: JSON.stringify(model)
            })
            .success(function(result) {
            });
    });

    </script>

Controller.cs

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

    [HttpPost]
    public ActionResult Index(CompanyRegister companyregister)
    {
        if (ModelState.IsValid)
        {
            // Use the incoming data
        }

        return View();
    }
}

public class CompanyRegister
{
    public string Name { get; set; }
    public string UsedForYear { get; set; }
}

【讨论】:

  • 错误警报正在调用,即错误函数正在调用
  • @dazzlingkumar - 这意味着 Ajax 调用失败,请确保您有一个使用 HttpPost 属性装饰的操作方法,并且它有一个类型为“SomeClass”且属性名称为“Maincompanyname”的参数。跨度>
  • 在控制器动作中设置断点,看看断点是否命中。如果确实意味着数据绑定成功。
  • 哇,动作方法正在调用!谢谢,如果你愿意,请给你的邮件ID
【解决方案2】:

我认为您正在使用 GET 请求,理想情况下应该是 POST 请求。

喜欢-

 $('#submit_fourth').click(function () {
alert('data sent');
$.ajax({
    url: "/Home/Index",
    data: { 'Id': groupId },
    type: 'POST',
    success: function (result) {
        //do the necessary updations
    },
    error: function (result) {
    }
});
});

【讨论】:

    【解决方案3】:

    解决方案 1:

    更改索引方法类型或从您尝试调用post 方法的脚本中创建新的[HTTPPOST]

    解决方案 2:(我更喜欢)

    将输入类型从submit更改为button,以避免多次提交。

    解决方案 3:

    在 html 代码中调用 Onclick 函数,如: &lt;input type="submit" OnClick=DataSubmit(); /&gt;

    脚本

    <script>
    DataSubmit()
    {
        //Do your stuff;
    };
    </script>
    

    【讨论】:

      【解决方案4】:

      将您的请求类型更改为“POST”,我认为您想插入一些数据,因此您必须调用 POST 方法并在您的操作方法上方插入“HTTPPOST”注释,如下所示:

      [HttpPost]
      public ActionResult Index(Models.Companyregister comreg)
      {
          if (ModelState.IsValid)
          {
      
      
              dataaccess.Companyregister_D ds = new dataaccess.Companyregister_D();
      
              int a = ds.insertcompanyregister(comreg);
      
                          }
         }
      

      【讨论】:

        【解决方案5】:

        您必须要使用 ajax 发送您的模型以执行您的操作必须具有 [HttpPost] 并在您的 ajax 调用代码中添加此

        //Ajax Call inClient
        $('#submit_fourth').click(function () {
        $.ajax({
        url: "/Home/CreateUser",
        data: $('#formId').Serialize(),
        type: 'POST',
        success: function (result) {
            //To DO
        }
        });
        });
        // Model
        public class User
        {
         public string UserName { get; set; }
         public string Code{ get; set; }
        }
        // Action
        [HttpPost]
          public ActionResult CreateUser(User model)
           {
              return View();
           }
        //View
        @using (Html.BeginForm( new { Id= "form" }))
        {
         <div class="form-group">
             @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
             <div class="col-md-10">
             @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
        
              </div>
             </div>
        <div class="form-group">
             @Html.LabelFor(m => m.Code, new { @class = "col-md-2 control-label" })
             <div class="col-md-10">
             @Html.TextBoxFor(m => m.Code, new { @class = "form-control" })
        
              </div>
              </div>
        <input type="button" id="submit_fourth"  value="Save" class="btn btn-default" />
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-01-10
          • 2011-01-31
          • 2013-07-28
          • 1970-01-01
          • 2018-08-24
          • 1970-01-01
          • 2020-12-05
          • 1970-01-01
          相关资源
          最近更新 更多