如何确定c# ASP中选择了哪个单选按钮.. Net core MVC 5.0



我想检查一下从我的Index.cshtml文件中选择了哪个单选按钮,从那里我想使用一个开关语句来设置其对象的属性。我正在阅读如何做到这一点,但他们中的大多数是在windows窗体组,但这并不适用于我,因为我使用的是asp.net core 5.0 web mvc项目。

在我的Index.cshmtl中我有以下内容:

....
<form action="@Url.Action("SecIndex", "Second")">
<input type="radio" value="1" /><label>Valid</label>
<input type="radio" value="2" /><label>Wrong</label>
<input type="radio" value="3" /><label>InValid</label>
<input type="submit" value="Address Validation" />
</form>

然后在我的HomeController中,我有以下方法:

[HttpPost]
public IActionResult ValidateAddress()
{
// if radio button was checked, perform the following var request.

var check = ""; // this should be able to grab the label of the button that was selected
switch (check)
{
case "Valid":
var request = new AddressRequest
{
StatusCode = 1,
Address = "2018 Main St New York City, NY, 10001"
};
break;
case "Wrong":
var req = new AddressRequest
{
StatusCode = 2,
Address = "91 Apple City Galveston, TX, 77550"
};
break;
case "Invalid":
var addressRequest = new AddressRequest
{
StatusCode = 3,
Address = "00000 Tree Ln, Miami FL 91041"
};
break;
}
return View();
}

但我似乎无法弄清楚如何允许变量check执行检查,看看哪个单选按钮被选中,因为这是一个asp.net core 5.0 web mvc项目,而不是一个窗口的形式。在这种类型的项目中有办法做到这一点吗?


更新:

所以我意识到我想要的值和以前的数字一样。所以实现这个建议,我仍然需要将用户重定向到Second控制器中的SecIndex方法。但是,我仍然需要获取选中的单选按钮的值。所以我尝试做return RedirectToAction("SecIndex");,但当我运行程序并点击按钮验证时,它给了我:

此页面无法正常工作。如果问题仍然存在,请联系站点所有者。HTTP错误405

我做错了什么吗?URI显示为https://localhost:5443/Home/ValidateAddress?Status=1。不是,我确实选择了第一个选项,它应该把我的状态显示为1。不知道为什么它返回这个。

Inside myHomeController:

[HttpPost]
public IActionResult ValidateAddress(string validate)
{
// if radio button was checked, perform the following var request.
switch (validate)
{
case "Valid":
var request = new AddressRequest
{
StatusCode = 1,
Address = "2018 Main St New York City, NY, 10001"
};
break;
case "Wrong":
var req = new AddressRequest
{
StatusCode = 2,
Address = "91 Apple City Galveston, TX, 77550"
};
break;
case "Invalid":
var addressRequest = new AddressRequest
{
StatusCode = 3,
Address = "00000 Tree Ln, Miami FL 91041"
};
break;
}
return RedirectToAction("SecIndex");
}
}

Home View... index.cshtml:

<form action="@Url.Action("ValidateAddress", "Home")">
<input type="radio" value="1" name="Status"/><label>Valid</label>
<input type="radio" value="2" name="Status"/><label>Wrong</label>
<input type="radio" value="3" name="Status"/><label>InValid</label>
<input type="submit" value="Address Validation" />
</form>

为单选按钮指定一个通用名称,并将标签文本分配给单选按钮的值。

changeIndex.cshmtlto:

<form method="post" action="/DemSecondo/ValidateAddress">
<input type="radio" value="Valid" name="myRadio" /><label>Valid</label>
<input type="radio" value="Wrong" name="myRadio" /><label>Wrong</label>
<input type="radio" value="InValid" name="myRadio" /><label>InValid</label>
<input type="submit" value="Address Validation" />
</form>

并将ValidateAddressaction更改为:

[HttpPost]
public IActionResult ValidateAddress(string myRadio)
{
switch (myRadio)
{
case "Valid":
var request = new AddressRequest
{
StatusCode = 1,
Address = "2018 Main St New York City, NY, 10001"
};
break;
case "Wrong":
var req = new AddressRequest
{
StatusCode = 2,
Address = "91 Apple City Galveston, TX, 77550"
};
break;
case "Invalid":
var addressRequest = new AddressRequest
{
StatusCode = 3,
Address = "00000 Tree Ln, Miami FL 91041"
};
break;
}
return RedirectToAction("SecIndex");
}

最新更新