Pass two values to view?
I have a query where I need to get post + every tag that it has. But since Tags are in separate table it makes things hard for me for two reasons. I am not really sure that I even need to pass two values, maybe I can do this somehow differently.
Note that Model is created automatically using LINQ to SQL Classes.
DB
Code
public ActionResult Edit(int id)
{
// Get post from DB
var post = (from p in _db.Posts
where p.ID == id
开发者_Python百科 select p).Single();
// Get PostTags from DB
var postTag = (from pt in _db.PostTags
where pt.PostID == id
select pt);
// Now looping through PostTags in order to find all tag names associated with the post
string tagList = string.Empty;
foreach (var pt in postTag)
{
var tag = (from t in _db.Tags
where t.ID == pt.TagID
select t).Single();
tagList += tag.Name + " ";
}
return Content(tagList); // This works as intended, outputs all tags for current post
return View(); // How can I return post itself + tagList ?
}
Problem
As you can see I want to pass
post
to my view and it makes sense, but I need to somehow pass thetagList
string that contains tags as well. How can I do that?I think my query is bad. I'm not experienced with SQL/LINQ so this is the only thing that I came up with. Maybe I can do the same thing somewhat "smarter"?
Okey, thank you for answers. Both ViewBag and creating BlogViewModel are good. I will go with BlogViewModel I think. But my second question still remains.
Have you considered creating a ViewModel that would hold both of these objects, and passing that ViewModel to your View? You can also use LINQ to improve your queries.
(Note : I'm posting this from my phone so the syntax could be off)
Model:
public class TaggingModel
{
public string TagList {get; set;}
public PostClass Post {get; set;}
}
public TaggingModel(string tagList, Post post)
{
this.TagList = tagList;
this.YourPost = post;
}
Code (using LINQ):
var post = _db.Posts.Where(pt => pt.PostID == id).Single();
var postTags = post.PostTags.Where(pt => pt.PostID == id);
string tagList = string.Empty;
foreach(var pt in postTags)
{
var tag = _db.Tags.Where(t => t.ID == pt.TagID).Single();
tagList += tag.Name + " ";
}
TaggingModel model = new TaggingModel(tagList, post);
return View(model);
You could also use other methods to send the message across, such as storing them in the ViewData if you didn't want to implement a Model for it.
You can use the ViewBag dynamic object or ViewData to pass one item, use second item as a model to the view. Also you can define a viewmodel class, or use a Tuple
ViewData["tags"] = tagsList;
return View(post);
or if you don't want to create a viewmodel but still want a strongly-typed view
return View(Tuple.Create(post, tagsList));
Define a View Model class that has a property for the post and a property for the tagList and pass it in the call to View().
I prefer to create a model that contains the two different objects and properties:
public class EditPageModel()
{
public PostClass Post (get; set;}
public string TagList {get; set;}
}
Then return this:
return View (new EditPageModel(){Post = post, TagList = tagList});
精彩评论