I have the folloiwng code inside my asp.net mvc action method:-
我在asp.net mvc动作方法中有以下代码: -
var CustomerData = customerlist.Select(m => new SelectListItem()
{
Text = m.SDOrganization.NAME,
Value = m.SDOrganization.ORG_ID.ToString(),
});
currently if i remove the ToString() from the ORG_ID , i will get an error that "can not explicitly convert long to string". so it seems that i have to define both the value and the text for the SelectListItem as strings. but since the SelectListItem should hold long , so is there a way to pass the values of the SelectListItem as long instead of strings?
目前,如果我从ORG_ID中删除ToString(),我将收到“无法显式地将long转换为字符串”的错误。所以我似乎必须将SelectListItem的值和文本定义为字符串。但由于SelectListItem应该保持很长时间,所以有没有办法传递SelectListItem的值而不是字符串?
1 个解决方案
#1
14
...so is there a way to pass the values of the SelectListItem as long instead of strings?
...有没有办法传递SelectListItem的值而不是字符串?
No. And it doesn't make any sense to do so as when it is rendered, it's just HTML which has no concept of long
.
没有。这样做没有任何意义,因为它只是渲染时,它只是HTML,没有长的概念。
If we have the action
如果我们有行动
public ActionResult Test()
{
var dictionary = new Dictionary<int, string>
{
{ 1, "One" },
{ 2, "Two" },
{ 3, "Three" }
};
ViewBag.SelectList = new SelectList(dictionary, "Key", "Value");
return this.View();
}
and the following view "Test.cshtml":
和以下视图“Test.cshtml”:
@using (Html.BeginForm())
{
@Html.DropDownList("id", ((SelectList)ViewBag.SelectList), "All")
<input type="submit" value="Go" />
}
The generated HTML is
生成的HTML是
<form action="/home/test" method="post">
<select id="id" name="id">
<option value="">All</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<input type="submit" value="Go">
</form>
and when we post to this action, text of your number is effectively parsed back into your desired type by the Model Binder
当我们发布此动作时,模型Binder会有效地将您的号码文本解析回您想要的类型
[HttpPost]
public ActionResult Test(int? id)
{
var selectedValue = id.HasValue ? id.ToString() : "All";
return Content(String.Format("You selected '{0}'", selectedValue));
}
And the above works as you might expect.
以上工作正如您所料。
#1
14
...so is there a way to pass the values of the SelectListItem as long instead of strings?
...有没有办法传递SelectListItem的值而不是字符串?
No. And it doesn't make any sense to do so as when it is rendered, it's just HTML which has no concept of long
.
没有。这样做没有任何意义,因为它只是渲染时,它只是HTML,没有长的概念。
If we have the action
如果我们有行动
public ActionResult Test()
{
var dictionary = new Dictionary<int, string>
{
{ 1, "One" },
{ 2, "Two" },
{ 3, "Three" }
};
ViewBag.SelectList = new SelectList(dictionary, "Key", "Value");
return this.View();
}
and the following view "Test.cshtml":
和以下视图“Test.cshtml”:
@using (Html.BeginForm())
{
@Html.DropDownList("id", ((SelectList)ViewBag.SelectList), "All")
<input type="submit" value="Go" />
}
The generated HTML is
生成的HTML是
<form action="/home/test" method="post">
<select id="id" name="id">
<option value="">All</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<input type="submit" value="Go">
</form>
and when we post to this action, text of your number is effectively parsed back into your desired type by the Model Binder
当我们发布此动作时,模型Binder会有效地将您的号码文本解析回您想要的类型
[HttpPost]
public ActionResult Test(int? id)
{
var selectedValue = id.HasValue ? id.ToString() : "All";
return Content(String.Format("You selected '{0}'", selectedValue));
}
And the above works as you might expect.
以上工作正如您所料。