Danger_Fox
Danger_Fox

Reputation: 449

Value not being passed properly to controller from view

I have an MVC application that when a link is clicked a page needs to be displayed based on the same values on another page. I can't figure out why what's getting passed is null instead of the string. My code is below.

Controller:

public string searchQ
    {
        get { return (string)Session["searchQ"]; }
        set { Session["searchQ"] = value; }
    }

    public ActionResult Index()
    {
        Session["InitialLoad"] = "Yes";
        return View();
    }


    [HttpPost]
    public ActionResult Index(string heatSearch)
    {
        ViewBag.SearchKey = heatSearch;
        searchQ = heatSearch;
        return View();
    }


    public ActionResult Index_Perm()
    {
        ViewBag.SearchKey = searchQ;

        return View();
    }






    public ActionResult PartialMainLim(string heatSearch)
    {
        HomeModel C = new HomeModel();
        ChemViewModel D = new ChemViewModel();
        D = C.QueryResults(heatSearch);

        return PartialView(D);
    }


    public ActionResult PartialMain(string heatSearch)
    {
        HomeModel C = new HomeModel();
        ChemViewModel D = new ChemViewModel();
        D = C.QueryResults(heatSearch);

        return PartialView(D);
    }

The code in the index view looks like this (this one works):

@if (ViewBag.SearchKey != null)
{
    <div>
    @Html.Action("PartialMainLim", "Home", (string)ViewBag.SearchKey)
    </div>
 }

And in the index_perm view:

@if(ViewBag.SearchKey != null)
{
    <div>
    @Html.Action("PartialMain", "Home", (string)ViewBag.SearchKey)
    </div>
 }

When I check the value of SearchKey in both views it is correct. However for the method "PartialMain" null gets passed instead of the string, despite SearchKey being correct. This all works for the other view though. What am I doing wrong?

Upvotes: 0

Views: 467

Answers (3)

Jakub
Jakub

Reputation: 139

When passing values back to controller you have basically two options:

  1. Make a form

  2. Pass it as part of the url

Get methods only accept url attributes while Post methods are able to handle form content as well.

From what you are trying to do I'd say you could use something like:

@Html.Action("PartialMain", "Home", new {heatSearch = (string)ViewBag.SearchKey})

this should create url looking like /Home/PartialMain?heatSearch=[content of SearchKey]

EDIT:

That will only pass the value given it is present in the ViewBag. You are getting it from the Session which is imho a terrible idea in MVC (which should be session-less). Please consider if you really need it there. Usually there are other ways to implement this.

Upvotes: 2

cat916
cat916

Reputation: 1361

I think that problem is your session that would be null. One of principles of framework ASP.NET MVC is stateless. Using session in ASP.NET MVC quite horrible.

At the moment, I think you can quickly fixed it by using TempData that default using Session under the hood. You could have a look an outdated article for further digging up ViewData vs TempData

Upvotes: 0

aquaraga
aquaraga

Reputation: 4168

There is no HttpPost handler in the controller when you click the index_perm view.

Upvotes: 0

Related Questions