0

Right, feels like a very noob questions but still new to all of this :)

I have a class in a library that is supposed to generate a doc based on a few variables that should be passed on from the MVC5 web app.

I looked at a couple of tutorials but I can't get my head around it so maybe I am approaching this the wrong way?

This is my model:

      public class SummaryTicketsReportModel
        {
            public bool ServiceDesk { get; set; }

            [DisplayFormat(DataFormatString = "{0:DD/MM/YYYY", ApplyFormatInEditMode = true)]
            [DataType(DataType.Date)]
            [DisplayName("From")]
            public DateTime StartDate { get; set; }

            [DisplayFormat(DataFormatString = "{0:DD/MM/YYYY", ApplyFormatInEditMode = true)]
            [DataType(DataType.Date)]
            [DisplayName("From")]
            public DateTime EndDate { get; set; }


    //Do I need this?
            //public SummaryTicketsReportModel ()
            //{
            //   StartDate = new DateTime();
            //    EndDate = new DateTime();
            //}

This is my controller:

public class SummaryReportController : Controller
    {
        // GET: SummaryReport
        public ActionResult Index()
        {

            return View();
        }

        //POST Action
        [HttpPost]
        public ActionResult Index(SummaryTicketsReportModel serviceDesk, SummaryTicketsReportModel startDate, SummaryTicketsReportModel endDate)
        {
            // takes in the view model
            var selectedServiceDesk = serviceDesk;
            var selectedStartDate = startDate;
            var selectedEndDate = endDate;
            var generateReport = new TicketSummaryReport();
//Needs to access the following: MonthSummaryReport ( ServiceDesk, StartDate, EndDate, summaryDocX) 
            //return generateReport.MonthsSummaryReport();
        }
    }

And this is my view:

@using System.Drawing
@model SummaryTicketsReportModel

@{
    ViewBag.Title = "TicketsSummaryReport";
}


<h2>TicketsSummaryReport</h2>

@using (Html.BeginForm())
{
    <tr>
        <td>
            @Html.TextBox("", String.Format("{0:d}", Model.StartDate))

        </td>
        <td>
            @Html.TextBox("", String.Format("{0:d}", Model.EndDate))
        </td>
        <td style="text-align: center">
            @Html.CheckBoxFor(model => model.ServiceDesk)

        </td>
    </tr>
    <input type="submit"/>
}
3
  • What is your question ? Oct 29, 2014 at 11:01
  • It kinda refuses to work :( like not even run due to "not all code returns a value"
    – Anca.N
    Oct 29, 2014 at 11:04
  • 1
    It looks like you are not returning anything from your controller action, what does your commented out method return? If it is a SummaryTicketsReportModel you probable want something like return View(generateReport.MonthsSummaryReport()); Oct 29, 2014 at 11:07

1 Answer 1

2

In order for MVC Model binding to work, the id's of the HTML form elements must match the property names of SummaryTicketsReportModel.

So you would need to do this:

@Html.TextBox("StartDate", String.Format("{0:d}", Model.StartDate))
@Html.TextBox("EndDate", String.Format("{0:d}", Model.EndDate))

Alternatively, to use the Annotation goodness that you have applied in your SummaryTicketsReportModel:

@Html.TextBoxFor(model => model.StartDate)
@Html.TextBoxFor(model => model.EndDate)

In your controller, try this:

[HttpPost]
public ActionResult Index(SummaryTicketsReportModel model)
{
    // takes in the view model
    var selectedServiceDesk = model.ServiceDesk;
    var selectedStartDate = model.StartDate;
    var selectedEndDate = model.EndDate;

    //The rest of your code

    return View();
}

I haven't tested this so hopefully there isn't anything else wrong.

1
  • Thank you! Helped me a bit however now I got to do fixed on some other things :) cheers!
    – Anca.N
    Oct 30, 2014 at 8:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.