Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
782 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - URL.Action includes id when constructing URL

I'm using ASP.Net MVC. Here's my code snippets from a controller named Course:

public ActionResult List(int id)
{
    var viewmodel.ShowUrl = Url.Action("Show", "Course");


    ...
}

public ActionResult Show(int id)
{
  ...
}

viewmodel.ShowUrl picks up whatever the value is of the "id" parameter. So viewmodel.ShowUrl becomes "/Course/Show/151" (value of id is 151); I want to be able to set the id part on the client based on user interaction. I want the value of viewmodel.ShowUrl to be "/Course/Show".

This seems like a bug to me. I'm not telling Url.Action to include an id value. It's doing it on its own. If I want to set the id value then I would do something like this:

var viewmodel.ShowUrl = Url.Action("Show", "Course", new {id = somevalue});

So, how do you prevent MVC from adding the id value? I can hardcode viewmodel.ShowUrl to "/Course/Show" but that seems to be a kludgy solution. Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just came across the same problem and so you know, you can also just use an empty string:

@Url.Action("Show", "Course", new { id = "" })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...