gpt4 book ai didi

c# - 从选择选项标签中检索文本(不是值)

转载 作者:行者123 更新时间:2023-11-30 16:03:55 24 4
gpt4 key购买 nike

我正在使用 asp.net c# mvc4

我目前可以从我的 View 中检索 Controller 中所选选项的值,但我还需要检索文本。我的 View 和 Controller 如下所示:

查看:

<form method="post" action="myAction">
<select name="sel">
<option value="1" text="One">One</option>
<option value="2" text="Two">Two</option>
</select>
</form>

Controller :

public ActionResult myAction(int sel) 
{
int selValue = sel;
}

这段代码对我来说很管用。但是,在我的 Controller 中,我想添加 string selText=something 这样我也有文本,代码看起来像:

public ActionResult myAction(int sel, string something) 
{
int selValue = sel;
string selText = something
}

谁能帮我解决这个问题?

最佳答案

通常您无法获取文本,因为表单仅发布选择的值。但是您可以从服务器(在模型或 ViewBag 中)动态填充选择列表,并且当您收到带有值的帖子时 - 获取与该值对应的文本。

这是一个非常常见的模式,不需要任何解释。你永远不会回发文本,因为你不需要它。

这里是代码:

public ActionResult myAction(int sel) 
{
int selValue = sel;
string text= yourRepository.GetById(sel)?.YourText;
}

关于c# - 从选择选项标签中检索文本(不是值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35993832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com