dimanche 19 avril 2015

multiple models in mvc view, no EditFor field showing


Vote count:

0




I have an issue, I am creating a blog based website and one of the things I wanted was to allow Tags on Posts. The relationship in my database and model is a many to many relationship between Post and Tags. However I am struggling to use tags in my Posts create view, the other issue I have is with Entity Framework and my SQL, because it is a many to many relationship no foreign key was assigned in either table. Just the association, when creating a post with a tag will my PostController handle it correctly?



@model MyBlogger.Post

@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<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.Tags)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Tags) // does not show on the webpage
@Html.ValidationMessageFor(model => model.Tags)
</div>


When I run the application no EditorFor field appears. Just the Label for it. I read that you have to decorate something with UIHint however I tried that and it did nothing.


PostController:



[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Post post, Tag tag)
{

int maxLength = 50;
var Title = post.Title;
string str = Title.ToLower();
// invalid chars, make into spaces
str = Regex.Replace(str, @"[^a-z0-9\s-]", "");
// convert multiple spaces/hyphens into one space
str = Regex.Replace(str, @"[\s-]+", " ").Trim();
// cut and trim it
str = str.Substring(0, str.Length <= maxLength ? str.Length : maxLength).Trim();
// hyphens
str = Regex.Replace(str, @"\s", "-");

var TitleTag = tag.Name;
string tagstr = Title.ToLower();
// invalid chars, make into spaces
tagstr = Regex.Replace(str, @"[^a-z0-9\s-]", "");
// convert multiple spaces/hyphens into one space
tagstr = Regex.Replace(str, @"[\s-]+", " ").Trim();
// cut and trim it
tagstr = str.Substring(0, str.Length <= maxLength ? str.Length : maxLength).Trim();
// hyphens
tagstr = Regex.Replace(str, @"\s", "-");

post.UrlSlug = str;
post.BlogUserEmail = User.Identity.Name;
post.PostedOn = DateTime.Now;

tag.UrlSlug = tagstr;
tag.Description = tag.Name;
try
{
if (ModelState.IsValid)
{
db.Tags.Add(tag);
db.Posts.Add(post);
//how do I make the association between the post and tag?
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", post.CategoryId);
return View(post);
}
catch (DbEntityValidationException e)
{
var newException = new FormattedDbEntityValidationException(e);
throw newException;
}

}


enter image description here


enter image description here



asked 20 secs ago







multiple models in mvc view, no EditFor field showing

Aucun commentaire:

Enregistrer un commentaire