在 asp.net 4.0 mvc 中显示视图数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8897632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:11:43  来源:igfitidea点击:

Display viewdata in asp.net 4.0 mvc

.netasp.net-mvcasp.net-mvc-3c#-4.0razor

提问by 14578446

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.

我试图做的是在数据库上成功执行查询后显示成功消息。一切正常,除了我的 viewdata 在视图页面上不显示任何内容。不知道为什么。下面是我的代码,请帮助我。

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>
}

回答by archil

You are using wrong method. First parameter of Labelmethod 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

你使用了错误的方法。Label方法的第一个参数是模型的属性名称。并且它生成带有属性 for="parameterValue" 的 html 标签,而不是带有该文本的标签。要向用户显示消息,您应该这样做

@ViewData["SuccessMessage"]

Also, take a look at TempData property

另外,看看TempData 属性

回答by Kevin Junghans

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 Helper 方法 Label。第一个参数始终是指示要显示的属性的表达式。第二个可选参数是要显示的文本。如果将其更改为以下内容,将显示 ViewData 中的文本。

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

回答by RaviTeja V

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; }

@{ 加载到变量 View Data["Student"] 作为您自己的模型;}

" @object.Name" display between the tags

"@object.Name" 显示在标签之间

**

**