Note that there are some explanatory texts on larger screens.

plurals
  1. POCan not pass data from a view to a controller
    primarykey
    data
    text
    <p>I started working with MVC and i am currently working on an application. I ran into a small problem when I'm trying to pass some data from a view to a controller action.</p> <p>This is my model :</p> <pre><code>public class TicketIndexModel { public IEnumerable&lt;Ticket&gt; ticketList { get; set; } public Ticket newTicket { get; set; } } </code></pre> <p>This is my controller action :</p> <pre><code>public ActionResult AddTicket(Ticket ticket) { string user = User.Identity.Name; TicketDetail ticketDetails = new TicketDetail(); if (ModelState.IsValid) { ticket.DateCreated = DateTime.Now; ticket.Status = "submitted"; ticket.UserName = user; db.Tickets.Add(ticket); db.SaveChanges(); //some other stuff return RedirectToAction("Index"); } </code></pre> <p>And in the view, first i show all the tickets and after that I have a small form to add a new ticket. This is how the form looks like.</p> <pre><code>@using (Html.BeginForm("AddTicket","Ticket",FormMethod.Post)) { &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.newTicket.Title) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.newTicket.Title) @Html.ValidationMessageFor(model =&gt; model.newTicket.Title) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.newTicket.Description) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.TextAreaFor(model =&gt; model.newTicket.Description, new { rows= "8",cols="2"}) @Html.ValidationMessageFor(model =&gt; model.newTicket.Description) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Submit" /&gt; &lt;/p&gt; } </code></pre> <p>The problem I am facing is when I try to add a new ticket, the ticket parameter is always null, so If(ModelState.Isvalid) &lt;- is always false and so i can not add new tickets to the DB.</p> <p>Does anyone have any ideas why ?</p> <p><strong>Later Edit</strong> :</p> <p>My model is now :</p> <pre><code>public IPagedList&lt;Ticket&gt; ticketList { get; set; } //[Required(ErrorMessage ="Title required")] [Display(Name = "Title")] public string TicketTitle { get; set; } //[Required(ErrorMessage="Description Required")] [Display(Name = "Description")] public string TicketDescription { get; set; } </code></pre> <p>My method is now : </p> <pre><code> [HttpPost] public ActionResult AddTicket(TicketIndexModel newTicket) { string user = User.Identity.Name; Ticket ticket = new Ticket(); if (ModelState.IsValid) { ticket.Title = newTicket.TicketTitle; ticket.Description = newTicket.TicketDescription; ticket.DateCreated = DateTime.Now; ticket.Status = "open"; ticket.UserName = user; db.Tickets.Add(ticket); db.SaveChanges(); </code></pre> <p>The problem i now have is the following :</p> <p>If in the model, i comment [Required] i can add a ticket, if not, in the controller, the newTicket in the signature is null every time i submit a new ticket. But, the controller enters the if(ModelState.IsValid) and i get an error at db.SaveChanges(ticket).</p> <p>I need both server and client side validation. Can you help me ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload