Retourner un beginform qui contient une référence à un deuxieme model.
Bonjour, j'ai un souci avec le retour d'un beginform (dans ma vue create).
Je code un site "type blog" et je souhaite créer coté back end une view createArticle. Seulement, mon article (qui correspond à une table Article dans ma base SQL) a dans ces variables, un référence (int : id) à un autre model qui est "Categorie" (également une autre table dans ma base de données)
la forme de mon formulaire :
-Titre : champ varchar
-Corp_article : champ text
-signature : champ varchar
-categorie : champ référence à la table "Categories" (un select list)
Mon code si ça aide :
Controller :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
[HttpGet]
public ActionResult CreateArticle()
{
using (EFRepository rep = new EFRepository())
{
List<SelectListItem> items = new List<SelectListItem>();
var listeCateg = rep.GetListeCategories();
foreach (var c in listeCateg)
{
SelectListItem itm = new SelectListItem();
itm.Text = c.nom;
itm.Value = c.id.ToString();
items.Add(itm);
}
ViewBag.Categories = items;
return View();
}
}
[HttpPost]
[ActionName("CreateArticle")]
public ActionResult CreateArticlePost(Article art)
{
using (EFRepository rep = new EFRepository())
{
rep.CreateArticle(art);
return View("Index");
}
} |
View CreateArticle :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
@model Microsouffle.Models.EF.Article
@{
ViewBag.Title = "CreateArticle";
}
<h2>CreateArticle</h2>
@using (Html.BeginForm("CreateArticle", "Home", FormMethod.Post, new { @class = "art" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Article</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.titre, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.titre, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.titre, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.corp_article, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.corp_article, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.corp_article, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.signature, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.signature, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.signature, "", new { @class = "text-danger" })
</div>
</div>
@* -------------------- Partie categirie --------------------------------- *@
<div class="form-group">
@Html.LabelFor(model => model.Category.nom, "Categorie", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Categories")
@*@Html.EditorFor(model => model.Category.nom, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.ValidationMessageFor(model => model.Category, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div> |
Model :
Article :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
public partial class Article
{
public Article()
{
this.Commentaires = new HashSet<Commentaire>();
}
public int id { get; set; }
public System.DateTime date_publi { get; set; }
public System.DateTime date_dermodif { get; set; }
public string titre { get; set; }
public string corp_article { get; set; }
public string signature { get; set; }
public virtual ICollection<Commentaire> Commentaires { get; set; }
public virtual Category Category { get; set; }
} |
Categorie :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
public partial class Category
{
public Category()
{
this.Articles = new HashSet<Article>();
}
public int id { get; set; }
public string nom { get; set; }
public string description { get; set; }
public Nullable<System.DateTime> date_crea { get; set; }
public Nullable<System.DateTime> date_modif { get; set; }
public virtual ICollection<Article> Articles { get; set; }
} |
Mon but est de passer a mon controller CreateArticle un model Article qui comporte la référence à la catégorie choisi.
Merci par avance.
Antoine