I’m working on an mvc .net web application. I hava a database that contains difficulties (a table called difficultes) and each difficulty has comments.
In my view I want to show every difficulty with its corresponding comments and a text area for a new comment and a button to submit it.
I created a model class that contains a difficulty and comments, a string var for the content of a new comment, and a string var for the name of the person who wants to comment. My problem is that I failed to submit the new comment.
Here is my model class
public class difficultecommentaires
{
public difficulte diff { get; set; }
public IList<commentaire> comms { get; set; }
public string pseudo { get; set; }
public string nouveau { get; set; }
}
Here is my action method for submitting the new comment
[HttpPost]
public ActionResult NewComment(int id_diff, string pseudo, string contenu)
{
difficulte d = new difficulte();
using (BDGestionEntities bd = new BDGestionEntities())
{
var query = from j in bd.difficultes where (j.id_diff == id_diff) select j;
foreach (var k in query)
d = k;
}
commentaire com = new commentaire();
com.pseudo = pseudo;
com.difficulte= d;
com.contenu = contenu;
db.AddTocommentaires(com);
db.SaveChanges();
ObtenirDifficulte(id_diff);
return View();
}
And here is my view
@model GestionProjet.Models.difficultecommentaires
@{
ViewBag.Title = "ObtenirDifficulte";
}
<h2>Détails</h2>
<fieldset>
<table>
<tr><td>
<label><b>Titre de la difficulté :</b></label></td><td>@Html.DisplayFor(m=>m.diff.titre)</td></tr>
<tr><td>
<label><b>Description :</b></label></td><td>@Html.DisplayFor(m=>m.diff.description)</td></tr>
</table>
<table>
@foreach (var k in Model.comms)
{
<tr><td>@k.pseudo a dit :</td><td>
@k.contenu</td></tr>
}
</table>
<br />
<table>
<tr><label><b>Nouveau commentaire</b></label></tr>
<tr>
<td><b>Nom :</b></td><td>@Html.TextBoxFor(m=>m.pseudo)</td>
</tr>
<tr>
<td><b>Commentaire :</b></td><td>@Html.TextAreaFor(m=>m.nouveau)</td>
</tr>
</table>
@Html.ActionLink("Ajouter", "NewComment", new { Model.diff.id_diff, Model.pseudo , Model.nouveau})
</fieldset>
<p>
<a href="@Url.Action("Index")"><img src="~/Images/retour.png" alt =""/></a>
</p>
And here is my model design (the difficulties part)

I think that the problem is in the parameters of my action link. I an error of null value.
How can I solve this.
By the looks of it you’re using an ActionLink which will perform a GET (passing the parameters you have given as route parameters, but not the form element values ie. the new comment.) Could try something like below:
And have your action accept: