Display viewdata in asp.net 4.0 mvc

What needs to be done is to show a success message after a successful query in the database. Everything works fine except for my viewdata, which do not display anything on the view page. I do not know why. Below is my code, please help me guys.

public class SearchItem
{
    [Required(ErrorMessage = "Required Field")]
    public string searchItem { get; set; }
}


    public ActionResult Index()
    {
        try
        {
            ViewData["SuccessMessage"] = "";
            return View();
        }
        catch (Exception ex)
        {
            return View("EmptySearch");
        }
    }

    [HttpPost]
    public ActionResult Index(string searchItem)
    {
        try
        {
             ............
            //database query with searchItem
            ...............

            string suceesstring = "A WAREHOUSE HOLD has been added.";
            ViewData["SuccessMessage"] = suceesstring;
            return View();
        }
        catch (Exception ex)
        {
            return View("EmptySearch");
        }
    }

And here is my view page:

@model KeleIntegratedTools.Models.SearchItem

@{
    ViewBag.Title = "Great Plains hold Insert Utility";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

< h2>Great Plains hold Insert Utility</h2>
< p class ="PenColor" >
Please enter order number to place on warehouse hold.

@using (Html.BeginForm("Index", "GreatPlains"))

{

< div>
    < fieldset>
        < legend>Order Information</legend>

        <div class="editor-label">
            @Html.Label("Order Number")

            @Html.TextBox("searchItem")
            @Html.ValidationMessageFor(m => m.searchItem)
            @Html.Label(ViewData["SuccessMessage"].ToString())
        </div>
        <p>
            <input type="submit" value="Search" />
        </p>
    </fieldset>
</div>
}
+5
source share
2 answers

You are using the wrong method. The first parameter to the Label method is the name of the model property. And it generates an html tag with the for = "parameterValue" attribute, and not a tag with this text. To display a message to the user, you must do this as

@ViewData["SuccessMessage"]

, TempData

+8

, Html Helper. , . - . , ViewData.

@Html.Label("", ViewData["SuccessMessage"].ToString())
+1

All Articles