如何在单击按钮上调用多个动作

时间:2014-07-01 10:52:59

标签: asp.net-mvc-4

我正在使用mvc来显示数据库中的数据我希望显示基于customerid和agentid的数据,其中用户输入customerid需要一个动作和agentid以及customerid用于另一个动作

public ActionResult Index(string ServiceProviderID)
    {
        var value = Convert.ToInt32(ServiceProviderID);
        var Query = (from u in db.collections
                 join pr in db.outstanding_master on u.CustomerID equals pr.LoanID
                     where pr.ServiceProviderID == value
                 select u);
        return View(Query);
    }

    public ActionResult AgentWise(string agentid)
    {

        var Query = (from u in db.collections

                     where u.AgentID == agentid
                     select u);
        return View(Query);

    }

查看

@using (Html.BeginForm("Index", "CollectionsReport"))
{      
@Html.CheckBox("ServiceProviderID",true)  
@Html.Label("ServiceProviderID", "ServiceProviderID")

@Html.CheckBox("AgentID",false)
@Html.Label("AgentID", "AgentID")
 <div class="editor-row">

    <div class="editor-field">
        @Html.EditorFor(model => model.ServiceProviderID)
    </div>
</div>
<input type="submit" value="submit" name="service" />

}

怎么做?是否有可能请给我另一种方法的建议?三江源

2 个答案:

答案 0 :(得分:1)

你有几个选择。

  • 如果客户和年龄相互排斥,那么首先您可以尝试找到客户,如果找到返回客户视图,否则找到代理商,如果找到返回代理商视图

  • 有两个单独的输入字段,一个用于客户ID,另一个用于代理ID,并将索引操作更改为接受两个参数,并根据提供的参数选择正确的视图。

答案 1 :(得分:0)

你不能用这种方式在一个表单中调用两个动作,你可以在一个动作中处理这个:

public ActionResult Index(string ServiceProviderID)
    {
        var value = Convert.ToInt32(ServiceProviderID);

        var Query = (from u in db.collections
                 join pr in db.outstanding_master on u.CustomerID equals pr.LoanID
                     where pr.ServiceProviderID == value
                 select u);

        if(Query != null)
             return View(Query);

        else
        {
       var result = (from u in db.collections

                     where u.AgentID == ServiceProviderID
                     select u);
        return View(result); 
        }

    }