gpt4 book ai didi

c# - 如何在 HTTPPOST 中读取 HTML.RadioButtonFor 的选定值?

转载 作者:太空宇宙 更新时间:2023-11-03 23:31:06 25 4
gpt4 key购买 nike

我在 SO 上阅读了很多答案,但是它不适合我。

我有模型:

class SomeOrder
{
public string TypeDrink {get; set;}
}

Controller :

public ViewResult Edit(int id)
{
SomeOrder se = newSomeOrder{ TypeDrink=3 };
return View(se);
}

并查看:

@Html.EditorForModel   @Html.RadioButtonFor(m=>m.TypeDrink, "1") Tea
@Html.RadioButtonFor(m=>m.TypeDrink, "2") Coffee
@Html.RadioButtonFor(m=>m.TypeDrink, "3") Juice

如何在 [HTTPPOST] 方法中读取单选按钮的选定值?在我的 HTTPPOST 方法中,存储的是预选值,而不是用户选择的值:

[HTTPPOST]
public ViewResult Edit(SomeOrder se)
{
string chosenValue=se.TypeDrink;// always the old selected value
}

最佳答案

您的 View 在单选按钮之前包含 @Html.EditorForModel()EditorForModel() 将为模型中的每个属性生成表单控件,因此它将为属性 TypeDrink 生成一个控件。根据应用于您的属性的属性以及您可能拥有的任何 EditorTemplates,它可能会在隐藏的输入中生成。

因为您的表单首先回发 EditorForModel 生成的输入的名称/对值,输入的值将绑定(bind)到您的模型,单选按钮的值将被忽略通过 DefaultModelBinder

从您的 View 中删除 EditorForModel,模型将根据单选按钮的值进行绑定(bind)。

关于c# - 如何在 HTTPPOST 中读取 HTML.RadioButtonFor 的选定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32176945/

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