简体   繁体   English

action方法在控制器中被调用两次

[英]action method is being called twice in the controller

The action in my MVC controller is being called twice from my jQuery code. 我的MVC控制器中的操作是从我的jQuery代码中调用两次。 After doing some investigation I found that the link is being triggered by jQuery as well as from the MVC view. 在做了一些调查之后,我发现链接是由jQuery以及MVC视图触发的。

$('.treeview-menu li a[name="environments"]').on("click", function () {
    debugger;
    var genericId = $(this).attr('id');
    if (genericId != undefined) {
        $.ajax({
            type: "GET",
            url: "/Configuration/Development",
            data: { 'genericId': genericId } ,
            datatype: "application/json",
            success: function (data) { }
        })
    }
    else {
        return;
    }
});
[HttpGet]
public ActionResult Development(DashBoardViewModel model, string genericId)
{            
    ViewBag.EnvironmentType = "Development";
    DeploymentConfiguration_DAL _deploymentDal = new DeploymentConfiguration_DAL();
    model.Configuration = _deploymentDal.FetchDeploymentConfigurationByEnvironmentID(Convert.ToInt32(Convert.ToInt32(genericId)));
    if (model.Configuration != null)
    {
        return View("Index", model);
    }
    return View("Index");            
}    

View where the links are being populated 查看正在填充链接的位置

<li>
    <a href="#"><i class="fa fa-circle-o"></i> Deployment Environment</a>
    <ul class="treeview-menu">
        @foreach (var item in ViewBag.Environments)
        {
            <li>
                <a href="@Url.Action(@item.Name,"Configuration")" id="@item.GenericMasterId" name="environments">
                    <i class="fa fa-circle-o"></i>
                    @item.Name
                </a>
            </li>                                                                                                                         
        }                          
    </ul>
</li>

The problem that I am facing is when the application runs and after clicking on any of the links that are being dynamically generated. 我面临的问题是应用程序运行时以及单击正在动态生成的任何链接之后。 The action method runs the first time and populates my model, but instead of displaying it in my view the action method is being hit again which populates null in the model because of which the text boxes are not being bound from the db values. action方法第一次运行并填充我的模型,但不是在我的视图中显示它,而是再次点击action方法,在模型中填充null ,因为文本框没有从db值绑定。

Please help me in getting this resolved. 请帮我解决这个问题。 Is there a way that I can access the id value for href in my action result without using jQuery? 有没有一种方法可以在不使用jQuery的情况下在我的动作结果中访问hrefid值?

Try to replace @Url.Action(@item.Name,"Configuration") for href="#" . 尝试为href =“#”替换@ Url.Action(@ item.Name,“Configuration”)

<a href="#" id="@item.GenericMasterId" name="environments">

This is why you have 2 calls to your controller, 1 in jquery click and another 1 when you click at link. 这就是为什么你有2个调用你的控制器,1个在jquery点击,另外1个点击链接。

As you've described in the question, what is happening is: 正如您在问题中描述的那样,发生的事情是:

  • the jquery runs fine (but your question code does nothing with the view) jquery运行正常(但你的问题代码对视图没有任何作用)
  • the link on the href the redirects the page to a new view href上的链接页面重定向到新视图

If you want to get the page and not redirect , you can: 如果您想获取页面而不是重定向 ,您可以:

  • change the a to a button type=button a更改为button type=button
  • return false from the event handler 从事件处理程序返回false
  • call event.preventDefault() from the event handler 从事件处理程序调用event.preventDefault()
  • remove the href= (by changing to a button) 删除href= (通过更改为按钮)
  • possibly change to a # as per other answer, but this is advised against and will have other side affects without one of the above changes as well. 可能会根据其他答案更改为# ,但建议不要这样做,并且如果没有上述更改之一,也会产生其他副作用。

If you want to redirect (not stay on the same page), passing in the id, as you say "without using jquery" then change the @Url.Action to use the routeValues overload: 如果你想重定向 (不要停留在同一页面上),传入id,如你所说的“不使用jquery”那么更改@Url.Action以使用routeValues重载:

<a href='@Url.Action(item.Name, "Configuration", new { genericId = item.GenericMasterId })' ... />

You can extend the new { genericId = to include your other model properties that you need (though id should be enough with some changes to your action). 您可以扩展new { genericId =以包含您需要的其他模型属性(尽管id应该足够您对操作进行一些更改)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM