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>
}
2 个解决方案
#1
8
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
你使用的是错误的方法。 Label方法的第一个参数是model的属性名称。它生成带有=“parameterValue”属性的html标签,而不是带有该文本的标签。要向用户显示消息,您应该这样做
@ViewData["SuccessMessage"]
Also, take a look at TempData property
另外,看看TempData属性
#2
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 Helper方法Label。第一个参数始终是一个表达要显示的属性的表达式。第二个可选参数是要显示的文本。如果将其更改为以下内容,将显示ViewData中的文本。
@Html.Label("", ViewData["SuccessMessage"].ToString())
#1
8
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
你使用的是错误的方法。 Label方法的第一个参数是model的属性名称。它生成带有=“parameterValue”属性的html标签,而不是带有该文本的标签。要向用户显示消息,您应该这样做
@ViewData["SuccessMessage"]
Also, take a look at TempData property
另外,看看TempData属性
#2
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 Helper方法Label。第一个参数始终是一个表达要显示的属性的表达式。第二个可选参数是要显示的文本。如果将其更改为以下内容,将显示ViewData中的文本。
@Html.Label("", ViewData["SuccessMessage"].ToString())