I have created a blog site where users can Post
a blog, in my model and database using Entity Framework there is a many-to-many relationship between Posts
and Tags
.
In my database I also have PostTagMap
, however I don't see this in my EF Model. Not sure if I need to. The PostTagMap
just contains the unique identifier for both Post and Tag nothing else.
(should be noted I created the database from the model)
On my Create Post
View (and controller) I want the users to have the option to create tags (along with there post) making sure that they are identified with one another. However I cant find a way to do this.
Post Model:
public partial class Post
{
public Post()
{
this.Tags = new HashSet<Tag>();
}
public int Id { get; set; }
public string BlogUserEmail { get; set; }
public int CategoryId { get; set; }
public string Title { get; set; }
public string ShortDescription { get; set; }
[AllowHtml]
public string Description { get; set; }
public string Meta { get; set; }
public string UrlSlug { get; set; }
public bool Published { get; set; }
public System.DateTime PostedOn { get; set; }
public Nullable<System.DateTime> Modified { get; set; }
public virtual BlogUser BlogUser { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Post</legend>
<div class="editor-label">
@Html.HiddenFor(model => model.BlogUserEmail, User.Identity.Name)
</div>
<div class="editor-field">
@Html.ValidationMessageFor(model => model.BlogUserEmail)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CategoryId, "Category")
</div>
<div class="editor-field">
@Html.DropDownList("CategoryId", String.Empty)
@Html.ValidationMessageFor(model => model.CategoryId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ShortDescription)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ShortDescription)
@Html.ValidationMessageFor(model => model.ShortDescription)
</div>
<div class="editor-label">
@Html.TextAreaFor(model => model.Description)
</div>
<div class="editor-field">
@Html.ValidationMessageFor(model => model.Description)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
Edit:
There is no way for me to use a EditorFor
field in my Create
View for tags. This is because the Create
view is bound to @Model MyBlogger.Post
.
Tags only show up as a collection and as such if I say:
@Html.EditorFor(model => m.Tags)
Then it shows blank in my view when rendered.
I have tried a foreach
however that did not work. For Instance trying this in my Create
view:
@foreach (var item in Model.Tags)
{
@Html.EditorFor(model => item.Name)
@Html.EditorFor(model => item.Description)
@Html.EditorFor(model => item.UrlSlug)
}
I get the error: Object reference not set to an instance of an object.