Mihai Tibrea
Mihai Tibrea

Reputation: 641

Can not pass data from a view to a controller

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.

This is my model :

public class TicketIndexModel
{
    public IEnumerable<Ticket> ticketList { get; set; }
    public Ticket newTicket { get; set; }

}

This is my controller action :

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");
        }

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.

@using (Html.BeginForm("AddTicket","Ticket",FormMethod.Post)) {

    <div class="editor-label">
        @Html.LabelFor(model => model.newTicket.Title)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.newTicket.Title)
        @Html.ValidationMessageFor(model => model.newTicket.Title)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.newTicket.Description)
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.newTicket.Description, new { rows= "8",cols="2"})
        @Html.ValidationMessageFor(model => model.newTicket.Description)
    </div>

      <p>
        <input type="submit" value="Submit" />
    </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) <- is always false and so i can not add new tickets to the DB.

Does anyone have any ideas why ?

Later Edit :

My model is now :

public IPagedList<Ticket> 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; }

My method is now :

    [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();

The problem i now have is the following :

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).

I need both server and client side validation. Can you help me ?

Upvotes: 2

Views: 859

Answers (1)

Crwydryn
Crwydryn

Reputation: 840

Your AddTicket method should look like this

[HttpPost]
public ActionResult AddTicket(TicketIndexModel ticketIndexModel)

Then access newTicket from the object passed into it.

Upvotes: 2

Related Questions