简体   繁体   English

在asp.net 4.0 mvc中显示viewdata

[英]Display viewdata in asp.net 4.0 mvc

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. 一切都工作正常,除了我的viewdata,在视图页面上没有显示任何内容。 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>
}

You are using wrong method. 你使用的是错误的方法。 First parameter of Label method is the name of property of model. Label方法的第一个参数是model的属性名称。 And it generates html label with attribute for="parameterValue", not the label with that text. 并且它生成带有=“parameterValue”属性的html标签,而不是带有该文本的标签。 To display message to user, you should do it like 要向用户显示消息,您应该这样做

@ViewData["SuccessMessage"]

Also, take a look at TempData property 另外,看看TempData属性

The problem is how you are using the Html Helper method Label. 问题是你如何使用Html Helper方法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. 如果将其更改为以下内容,将显示ViewData中的文本。

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

Here i am providing some example to get better understanding. 在这里,我提供一些例子来更好地理解。

inherent your models here @using Mvc Project.Models 这里固有你的模型@using Mvc Project.Models

@{ load into variable View Data["Student"] as Your Own Model; @ {加载到变量查看数据[“学生”]作为您自己的模型; } }

" @object.Name" display between the tags 标签之间显示“@ object.Name”

** **

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM