Answer the question
In order to leave comments, you need to log in
How to save data in Asp.net MVC when the relationship is many to many?
Hello.
Please tell me how to save tags for news tags in texbox separated by commas.
There are two tables
public class News
{
public Int64 NewsId { get; set; }
[DisplayName("Название новости")]
[Required(ErrorMessage = "Заполните название новости")]
public string Title { get; set; }
[DisplayName("Краткое содержание новости")]
[Required(ErrorMessage = "Заполните краткое содержание")]
[DataType(DataType.MultilineText)]
[AllowHtml]
public string Abstract { get; set; }
[DisplayName("Новость")]
[Required(ErrorMessage = "Заполните новость")]
[DataType(DataType.MultilineText)]
[AllowHtml]
public string Body { get; set; }
[DisplayName("Дата добавления сообщения")]
[Required(ErrorMessage = "Заполните дату")]
public DateTimeOffset AddedDate { get; set; }
[DisplayName("Сообщение одобрено")]
public bool IsApproved { get; set; }
public virtual ICollection<Tags.Tags> Tags { get; set; }
}
public class Tags
{
[Key]
public string Tag { get; set; }
[DisplayName("Дата добавления тега")]
public DateTimeOffset AddedDate { get; set; }
public virtual ICollection<News.News> News { get; set; }
}
public async Task<ActionResult> Create([Bind(Include = "NewsId,Title,Abstract,Body")] News news)
{
if (ModelState.IsValid)
{
news.AddedDate = DateTimeOffset.Now;
db.News.Add(news);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(news);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question