6

What ia m trying to do is display a success message after the query has been executed succesfully on database. Everything is working fine except my viewdata which does not display anything on view page. Not sure 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>
}

3 Answers 3

9

You are using wrong method. First parameter of Label method is the name of property of model. And it generates html label with attribute for="parameterValue", not the label with that text. To display message to user, you should do it like

@ViewData["SuccessMessage"]

Also, take a look at TempData property

Sign up to request clarification or add additional context in comments.

3 Comments

thanks @archil, dis small problem was bugging me since long n being a beginner in mvc was not able to get it. thanks n appriciate your help :)
Additionally you should be using ViewBag, not ViewData. Please go through my intro to MVC, the the EF/MVC tutorials at asp.net/mvc
@RickAnderson-MSFT actually, he should not use ViewBag/ViewData at all. For message display, as I indicated, he should use TempData. in all the other cases, ViewModels only.
1

The problem is how you are using the Html Helper method Label. The first argument is always an expression that indicates the properties to display. The second optional argument is the text to display. If you change it to the following the text in your ViewData will display.

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

Comments

0

Here i am providing some example to get better understanding.

inherent your models here @using Mvc Project.Models

@{ load into variable View Data["Student"] as Your Own Model; }

" @object.Name" display between the tags

**

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.