Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have the following view:
HTML
@model ReportViewer.Models.ViewModel.EntryViewModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div> 
        @using (Html.BeginForm("UserReportView", "HomeController", FormMethod.Post))
        {
            <input id="UserToLookUp" type="text" width="50" name="UserToLookUp" />
            @Html.TextBoxFor(model => model.UserToLookUp)
            <input type = "submit" value = "View" />
        }
    </div>
</body>
</html>


It uses the following model:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ReportViewer.Models.ViewModel
{
    public class EntryViewModel
    {
        public string CurrentUser { get; set; }
        public string UserToLookUp { get; set; }
    }
}


My controller is as follows:
C#
using ReportViewer.Models.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ReportViewer.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            EntryViewModel vm = new EntryViewModel();
            return View(vm);
        }

        //[HttpPost]
        public ActionResult UserReportView(EntryViewModel vm)
        {
            TempData["ID"] = vm.UserToLookUp;

            return RedirectToAction("Index", "Viewer");
        }
    }
}


I need to get the value from the "UserToLookUp" textbox in to the TempData["ID"] of the ActionResult UserReportView. The vm is accessable in the method but UserToLookUp is always null.


What I have tried:

I've spent the better part of the last two days googling and trying every permutation of things I've seen in various tutorials and can not get it to work.
Posted
Updated 5-Jul-17 17:58pm
Comments
j snooze 5-Jul-17 17:31pm    
If you debug and set a break point, you can verify property in the vm object in your httppost method is empty? Why do you have html markup for userlookup and an mvc html helper for it? Are you just trying both to see what sticks? If your manual html markup I think the name has to be "Object.Property" so in your case "EntityViewModel.UserLookup" for the inner workings of MVC to recognize what to post back in the proper place.

1 solution

Corrections
@using (Html.BeginForm("UserReportView", "HomeController", FormMethod.Post))

<input id="UserToLookUp" type="text" width="50" name="UserToLookUp" />
 
Share this answer
 
Comments
jmallory0001 6-Jul-17 14:57pm    
Thank you so much! I figured it would be something relatively simple.
Karthik_Mahalingam 7-Jul-17 11:50am    
welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900