Working with Non-String Data

Our AddPost() action so far.

[ActionName("Add"), HttpPost]
public ActionResult AddPost(string date, string activityId, 
    string duration, string intensity, string exclude, string notes)
{
    ViewBag.Date = date;
    ViewBag.Activity = activityId;
    ViewBag.Duration = duration;
    ViewBag.Intensity = intensity;
    ViewBag.Exclude = exclude;
    ViewBag.Notes = notes;

    return View();
}

How if we want converting the input to non-string variable?

[ActionName("Add"), HttpPost]
public ActionResult AddPost(DateTime date, int activityId, 
    double duration, Entry.IntensityLevel intensity, bool exclude, string notes)
{
    ViewBag.Date = date;
    ViewBag.Activity = activityId;
    ViewBag.Duration = duration;
    ViewBag.Intensity = intensity;
    ViewBag.Exclude = exclude;
    ViewBag.Notes = notes;

    return View();
}

Try to run the project and leave the form blank and submit the form. Error huh? It says that our form is returning null value.

Luckily, in .NET we could do something like this.

[ActionName("Add"), HttpPost]
public ActionResult AddPost(DateTime? date, int? activityId, 
    double? duration, Entry.IntensityLevel? intensity, bool? exclude, string notes)
{
    ViewBag.Date = date;
    ViewBag.Activity = activityId;
    ViewBag.Duration = duration;
    ViewBag.Intensity = intensity;
    ViewBag.Exclude = exclude;
    ViewBag.Notes = notes;

    return View();
}

results matching ""

    No results matching ""