我想要smth。像这样。当用户输入mydomain.com/Message/时,他会获得所有的消息线程,但当他输入mydomain.com/Message/John时,他会在他和john之间获得消息线程。我不知道如何为此实现视图这里是控制器:
public ActionResult Index(String userName)
{
var user = UserRepository.GetUserByDisplayName(userName);
if (user == null || user.UserId.Equals(WebSecurity.CurrentUserId))
{
IQueryable<MessageThread> messageThreads =
MessageThreadRepository.GetMessageThreadsForUser(WebSecurity.CurrentUserId);
return View(messageThreads);
}
MessageThread thread = MessageThreadRepository.GetThreadBetweenUsers(WebSecurity.CurrentUserId, userName);
return View(thread);
}
答案 0 :(得分:2)
检查View方法的重载http://msdn.microsoft.com/en-us/library/dd460310(v=vs.98).aspx
为所有案例创建视图,并将viewName作为参数传递给View(“viewName”,yourObject)
答案 1 :(得分:0)
我不知道你的方法是什么或如何,但我希望它会对你有帮助..
您的网址是http://mydomain.com/Message?userName=John
private DatabaseEntities db = new DatabaseEntities();
public ActionResult Index(String userName)
{
IEnumerable<Message> messages = db.Messages;
if (!String.IsNullOrWhiteSpace(userName))
{
messages = messages.Where(x => x.UserName == userName);
}
return View(messages);
}
但不要忘记使用ViewModel ..