英文:
Id not being passed back to controller
问题
以下是您要翻译的部分:
"The manageuser post back method is being called but the UserId is not being passed it comes back as 0. How do I correct this issue?"
管理用户的后端方法被调用,但UserId未传递,它返回为0。我该如何纠正这个问题?
The html from view
<input type="image"
src="~/icons/bootstrap-icons-1.10.2/bootstrap-icons.css"
alt=""
class="bi bi-check-circle-fill fs-6"
name="btnDelete"
aria-label="Delete user"
onclick="location.href='@Url.Action("ManageUser", "ManageUsers", new {@executeAction = "Delete", @userId = @item.UserID})'" />
来自视图的HTML代码:
<input type="image"
src="~/icons/bootstrap-icons-1.10.2/bootstrap-icons.css"
alt=""
class="bi bi-check-circle-fill fs-6"
name="btnDelete"
aria-label="删除用户"
onclick="location.href='@Url.Action("ManageUser", "ManageUsers", new { executeAction = "Delete", userId = @item.UserID })'" />
The controller code
[HttpGet]
public IActionResult ManageUser(string Status, string? StatusMessage)
{
TempData["Message"] = "";
if(StatusMessage != null)
{
TempData["Message"] = StatusMessage;
}
//加载模型
return View(model);
}
[HttpPost]
public IActionResult ManageUser(string executeAction, int userId)
{
string status = executeAction;
string statusMessage = string.Empty;
if(executeAction == "Delete")
{
//删除用户
}
return RedirectToAction("ManageUser", "ManageUsers", new { Status = status, StatusMessage = statusMessage });
}
控制器代码:
[HttpGet]
public IActionResult ManageUser(string Status, string? StatusMessage)
{
TempData["Message"] = "";
if(StatusMessage != null)
{
TempData["Message"] = StatusMessage;
}
//加载模型
return View(model);
}
[HttpPost]
public IActionResult ManageUser(string executeAction, int userId)
{
string status = executeAction;
string statusMessage = string.Empty;
if(executeAction == "Delete")
{
//删除用户
}
return RedirectToAction("ManageUser", "ManageUsers", new { Status = status, StatusMessage = statusMessage });
}
英文:
The manageuser post back method is being called but the UserId is not being passed it comes back as 0. How do I correct this issue?
The html from view
<input type="image"
src="~/icons/bootstrap-icons-1.10.2/bootstrap-icons.css"
alt=""
class="bi bi-check-circle-fill fs-6"
name="btnDelete"
aria-label="Delete user"
onclick="location.href='@Url.Action("ManageUser", "ManageUsers", new {@executeAction = "Delete", @userId = @item.UserID})'" />
The controller code
[HttpGet]
public IActionResult ManageUser(string Status, string? StatusMessage)
{
TempData["Message"] = "";
if(statusMessage != null)
{
TempData["Message"] = statusMessage;
}
//load model
return View(model)
}
[HttpPost]
public IActionResult ManageUser(string executeAction, int userId)
{
string status = action;
string statusMessage = string.Empty;
if(executeAction == "Delete")
{
//delete the user
}
return RedirectToAction("ManageUser", "ManageUsers", status, statusMessage);
}
答案1
得分: 1
以下是翻译好的部分:
The manageuser post back method is being called but the UserId is not
being passed it comes back as 0. How do I correct this issue?
管理用户的回调方法被调用,但UserID没有传递,返回为0。我该如何纠正这个问题?
Well, as you may know Tag Helpers class generate link on page. Thus, we have either need to send request using query string or define get method on your controller action.
嗯,正如你所知道的,Tag Helpers类在页面上生成链接。因此,我们要么需要使用查询字符串发送请求,要么在控制器操作上定义GET方法。
If you could seen in tag helper class service reference it doesn't allow httpverb by default because it takes request as URL that's query string.
如果你在标签帮助类服务引用中看到,它默认不允许httpverb,因为它将请求作为查询字符串。
Solution:
解决方案:
To resolve the issue either we have to define controller action as GET, just as following:
要解决这个问题,我们要么将控制器操作定义为GET,就像下面这样:
Controller:
控制器:
public IActionResult ManageUser(string executeAction, int userId)
{
string status = "";
string statusMessage = string.Empty;
if (executeAction == "Delete")
{
//delete the user
}
return RedirectToAction("ManageUser", "ManageUsers", status, statusMessage);
}
View:
视图:
<input type="button"
src="~/icons/bootstrap-icons-1.10.2/bootstrap-icons.css"
alt=""
class="bi bi-check-circle-fill fs-6"
name="btnDelete"
value="Delete User"
aria-label="Delete user"
onclick="location.href='@Url.Action("ManageUser", "Some", new {@executeAction = "Delete", @userId = "111"})'" />
Note: Everything would remain same as yours.
**注意:**一切都将保持与你的一样。
Output:
输出:
Alternative Way:
另一种方式:
We can directly pass our parameter using location.href what more is, it will send controller parameter as query string and will act accordingly.
我们可以直接使用location.href传递参数,它将将控制器参数作为查询字符串发送,并相应地进行操作。
Note: If you need more information about ASP.NET Core Tag Helpers, you can have a look on official document here.
**注意:**如果你需要更多关于ASP.NET Core标签帮助的信息,你可以在这里查看官方文档。
英文:
> The manageuser post back method is being called but the UserId is not
> being passed it comes back as 0. How do I correct this issue?
Well, as you may know Tag Helpers class generate link on page. Thus, we have either need to send request using query string or define get method on your controller action.
If you could seen in tag helper class service reference it doesn't allow httpverb by default because it takes request as URL that's query string.
Solution:
To resolve the issue either we have to define controller action as GET, just as following:
Controller:
public IActionResult ManageUser(string executeAction, int userId)
{
string status = "";
string statusMessage = string.Empty;
if (executeAction == "Delete")
{
//delete the user
}
return RedirectToAction("ManageUser", "ManageUsers", status, statusMessage);
}
View:
<input type="button"
src="~/icons/bootstrap-icons-1.10.2/bootstrap-icons.css"
alt=""
class="bi bi-check-circle-fill fs-6"
name="btnDelete"
value="Delete User"
aria-label="Delete user"
onclick="location.href='@Url.Action("ManageUser", "Some", new {@executeAction = "Delete", @userId = "111"})'" />
Note: Everything would remain same as yours.
Output:
Alternative Way:
We can directly pass our parameter using location.href what more is, it will send controller parameter as query string and will act accordingly.
Note: If you need more information about ASP.NET Core Tag Helpers, you can have a look on official document here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论