如何确定在 C# ASP.NET CORE MVC 5.0 中选择了哪个单选按钮
How to determine which radio button was selected in C# ASP.NET CORE MVC 5.0
我想查看 select 从我的 Index.cshtml
文件中编辑了哪个单选按钮,然后我想从那里使用 switch 语句为其对象设置属性。我正在阅读有关如何执行此操作的信息,但其中大多数是针对 windows 形式的组,但这不适用于我,因为我使用的是 asp.net 核心 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
执行检查以查看哪个单选按钮被 selected,因为这是一个 asp.net 核心 5.0 web mvc项目而不是 windows 表格。有没有办法在这种类型的项目中做到这一点?
更新:
所以我意识到我想拥有与以前一样的数值。因此,在实施该建议时,我仍需要将用户重定向到我的 Second
控制器中的方法 SecIndex
。但是,我仍然需要获取 selected 的单选按钮的值。所以我试图做 return RedirectToAction("SecIndex");
但是当我 运行 程序并点击按钮验证它给了我:
This page isn't working. If the problem continues, contact the site owner.
HTTP ERROR 405
我是不是做错了什么? URI 显示为 https://localhost:5443/Home/ValidateAddress?Status=1
。不是我做了 select 第一个选项,它应该给我 Status 作为 1。不知道为什么它会返回那个。
我的HomeController
里面:
[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>
为单选按钮指定一个通用名称,并将标签文本分配给单选按钮的值。
将 Index.cshmtl
更改为:
<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>
并将 ValidateAddress
操作更改为:
[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");
}
我想查看 select 从我的 Index.cshtml
文件中编辑了哪个单选按钮,然后我想从那里使用 switch 语句为其对象设置属性。我正在阅读有关如何执行此操作的信息,但其中大多数是针对 windows 形式的组,但这不适用于我,因为我使用的是 asp.net 核心 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
执行检查以查看哪个单选按钮被 selected,因为这是一个 asp.net 核心 5.0 web mvc项目而不是 windows 表格。有没有办法在这种类型的项目中做到这一点?
更新:
所以我意识到我想拥有与以前一样的数值。因此,在实施该建议时,我仍需要将用户重定向到我的 Second
控制器中的方法 SecIndex
。但是,我仍然需要获取 selected 的单选按钮的值。所以我试图做 return RedirectToAction("SecIndex");
但是当我 运行 程序并点击按钮验证它给了我:
This page isn't working. If the problem continues, contact the site owner. HTTP ERROR 405
我是不是做错了什么? URI 显示为 https://localhost:5443/Home/ValidateAddress?Status=1
。不是我做了 select 第一个选项,它应该给我 Status 作为 1。不知道为什么它会返回那个。
我的HomeController
里面:
[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>
为单选按钮指定一个通用名称,并将标签文本分配给单选按钮的值。
将 Index.cshmtl
更改为:
<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>
并将 ValidateAddress
操作更改为:
[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");
}