Asp.net mvc 带参数的服务器端函数

Asp.net mvc 带参数的服务器端函数,asp.net-mvc,Asp.net Mvc,我很好奇,如何将参数传递给服务器端函数,如下所示 <asp:LinkButton ID="lkbChange" runat="server" OnClick="lkbChange_Click" /> 然后服务器端函数将如下所示: <script runat="server"> protected void lkbChange_Click(Object sender, EventArgs e) { Session["location"] = new_value;

我很好奇,如何将参数传递给服务器端函数,如下所示

<asp:LinkButton ID="lkbChange" runat="server" OnClick="lkbChange_Click" />
然后服务器端函数将如下所示:

<script runat="server">
protected void lkbChange_Click(Object sender, EventArgs e)
{
    Session["location"] = new_value;
}
<script >

受保护的无效lkbChange\u单击(对象发送方,事件参数e)
{
会话[“位置”]=新的_值;
}
我想要的是:

<asp:LinkButton ID="lkbChange" runat="server" OnClick="lkbChange_Click(<%= value %>)" />

protected void lkbChange_Click(Object sender, EventArgs e, string value)
{
    Session["location"] = value;
}

受保护的无效lkbChange\u单击(对象发送方、事件参数e、字符串值)
{
会话[“位置”]=值;
}
因此,将参数传递给函数调用也存在问题…

编辑


好的,我开始觉得runat=“server”是错误的。嗯,我正在介绍一些机器和它们的特性。这里我想要的是根据这些机器的位置过滤当前结果(机器的总内容、历史记录等)。这意味着当前操作应保持不变-计算相同的结果集,仅向下过滤到从链接获取的当前选定位置,从而将会话[“位置”]的值更改为例如无处不在。

值是什么?这是LinkButton的某些属性吗?如果是,则您可以通过发件人访问它


如果是其他内容,那么AFAIK可以使用您提到的方式,也可以编写自定义事件处理程序。

什么是值?这是LinkButton的某些属性吗?如果是,则您可以通过发件人访问它


如果是其他内容,那么AFAIK可以使用您提到的方式,也可以编写自定义事件处理程序。

您可以使用CommandArgument和CommandName来实现这一点

<asp:LinkButton ID="lkbChange" runat="server" CommandArgument="MyArg"   
CommandName="ThisLnkClick" OnClick="MyHandler" />

然后,您必须创建接受参数的自定义方法。

您可以使用CommandArgument和CommandName来实现这一点

<asp:LinkButton ID="lkbChange" runat="server" CommandArgument="MyArg"   
CommandName="ThisLnkClick" OnClick="MyHandler" />

然后,您必须创建接受参数的自定义方法。

好的,我仍然不清楚您实际需要什么,但我会尝试一下(尽管我担心它会引发更多的问题而不是答案;-)

在您的global.asax.cs中添加类似以下内容的路线:

routes.MapRoute(
    "MachineList",
    "Machines/AddLocationFilter/{filterValue}",
    new { controller = "Machines", action = "AddLocationFilter", filterValue = "" }
);
然后,视图中的所有链接都是这样的(其中“value”是您的筛选值):


现在在您的机器控制器中:

public ActionResult Index()
{
    //Get your machines here
    //NOTE: I have no idea how you want this to work and this is just an example of how it could work
    IQueryable<Machine> machines = myMachineRepository.GetAllMachines();

    //Use the "FilterLocations" session value if it exists
    if (Session["FilterLocations"] != null)
    {
        IList<string> filterLocations = Session["FilterLocations"] as List<string>
        machines.Where(m => Locations.Contains(m.Location))
    }

    //Now send the filtered list of machines to your view
    return View(machines.ToList());
}

public ActionResult AddLocationFilter(string filterValue)
{
    //If there isn't a filterValue don't do anything
    if (string.IsNullOrEmpty(filterValue))
        return RedirectToAction("index");

    IList<string> filterLocations;
    //Get it from session
    if (Session["FilterLocations"] != null)
        filterLocations = Session["FilterLocations"] as List<string>

    //If it's still null create a new one
    if (filterLocations == null)
        filterLocations = new List<string>();

    //If it doesn't already contain the value then add it
    if (!filterLocations.Contains(filterValue))
        filterLocations.Add(filterValue);

    //Finally save it to the session
    Session["FilterLocations"] = filterLocations;

    //Now redirect
    return RedirectToAction("index");
}
public ActionResult Index()
{
//把你的机器拿来
//注意:我不知道您希望它如何工作,这只是它如何工作的一个示例
IQueryable machines=myMachineRepository.GetAllMachines();
//使用“FilterLocations”会话值(如果存在)
if(会话[“FilterLocations”]!=null)
{
IList filterLocations=会话[“filterLocations”]作为列表
机器。其中(m=>Locations.Contains(m.Location))
}
//现在将筛选的机器列表发送到您的视图
返回视图(machines.ToList());
}
公共操作结果AddLocationFilter(字符串filterValue)
{
//如果没有filterValue,请不要执行任何操作
if(string.IsNullOrEmpty(filterValue))
返回操作(“索引”);
IList过滤器位置;
//从会话中获取它
if(会话[“FilterLocations”]!=null)
filterLocations=会话[“filterLocations”]作为列表
//如果仍然为空,则创建一个新的
如果(filterLocations==null)
filterLocations=新列表();
//如果尚未包含该值,则添加该值
如果(!filterLocations.Contains(filterValue))
filterLocations.Add(filterValue);
//最后将其保存到会话中
会话[“FilterLocations”]=FilterLocations;
//现在重定向
返回操作(“索引”);
}
精明

我对最后的重定向并不完全满意,但我这样做是为了简单,试图帮助您更容易地理解事情。使用jQuery并返回JSON结果,您可以做一些更加精简和性感的事情。。。但这是另一个层次

HTHs

查尔斯

好吧,我对你真正需要什么还不太清楚,但我会试一试(尽管我担心这会引起更多的问题而不是答案;-)

在您的global.asax.cs中添加类似以下内容的路线:

routes.MapRoute(
    "MachineList",
    "Machines/AddLocationFilter/{filterValue}",
    new { controller = "Machines", action = "AddLocationFilter", filterValue = "" }
);
然后,视图中的所有链接都是这样的(其中“value”是您的筛选值):


现在在您的机器控制器中:

public ActionResult Index()
{
    //Get your machines here
    //NOTE: I have no idea how you want this to work and this is just an example of how it could work
    IQueryable<Machine> machines = myMachineRepository.GetAllMachines();

    //Use the "FilterLocations" session value if it exists
    if (Session["FilterLocations"] != null)
    {
        IList<string> filterLocations = Session["FilterLocations"] as List<string>
        machines.Where(m => Locations.Contains(m.Location))
    }

    //Now send the filtered list of machines to your view
    return View(machines.ToList());
}

public ActionResult AddLocationFilter(string filterValue)
{
    //If there isn't a filterValue don't do anything
    if (string.IsNullOrEmpty(filterValue))
        return RedirectToAction("index");

    IList<string> filterLocations;
    //Get it from session
    if (Session["FilterLocations"] != null)
        filterLocations = Session["FilterLocations"] as List<string>

    //If it's still null create a new one
    if (filterLocations == null)
        filterLocations = new List<string>();

    //If it doesn't already contain the value then add it
    if (!filterLocations.Contains(filterValue))
        filterLocations.Add(filterValue);

    //Finally save it to the session
    Session["FilterLocations"] = filterLocations;

    //Now redirect
    return RedirectToAction("index");
}
public ActionResult Index()
{
//把你的机器拿来
//注意:我不知道您希望它如何工作,这只是它如何工作的一个示例
IQueryable machines=myMachineRepository.GetAllMachines();
//使用“FilterLocations”会话值(如果存在)
if(会话[“FilterLocations”]!=null)
{
IList filterLocations=会话[“filterLocations”]作为列表
机器。其中(m=>Locations.Contains(m.Location))
}
//现在将筛选的机器列表发送到您的视图
返回视图(machines.ToList());
}
公共操作结果AddLocationFilter(字符串filterValue)
{
//如果没有filterValue,请不要执行任何操作
if(string.IsNullOrEmpty(filterValue))
返回操作(“索引”);
IList过滤器位置;
//从会话中获取它
if(会话[“FilterLocations”]!=null)
filterLocations=会话[“filterLocations”]作为列表
//如果仍然为空,则创建一个新的
如果(filterLocations==null)
filterLocations=新列表();
//如果尚未包含该值,则添加该值
如果(!filterLocations.Contains(filterValue))
filterLocations.Add(filterValue);
//最后将其保存到会话中
会话[“FilterLocations”]=FilterLocations;
//现在重定向
返回操作(“索引”);
}
精明

我对最后的重定向并不完全满意,但我这样做是为了简单,试图帮助您更容易地理解事情。使用jQuery并返回JSON结果,您可以做一些更加精简和性感的事情。。。但这是另一个层次

HTHs

Charles

值不是linkbutton的属性,而只是linkbutton应该保存在会话变量中的一个值,但是感谢您对该属性的了解。我做了