【问题标题】:Parameter passed to post controller is always null传递给后控制器的参数始终为空
【发布时间】:2017-01-01 01:09:37
【问题描述】:

这个问题与我的上一个问题非常相似,但这与传递多个参数有关。我的控制器有两个具有相同名称的方法,但是一个传递了一个可以为空的 int 来确定要在 post 上执行哪个操作(我不知道这是否是一个好的设计决策)。代码如下:

public ActionResult EventDetails(int? eventID)
{
    EventDetailsViewModel model = new EventDetailsViewModel();

     if (eventID != null)
     {
      model.afterPost = false;
      model.currentAttendance = getCountAttending(eventID);
      model.currentUser = getCurrentUserProfile(User.Identity.Name);
      model.eventDetails = getEventDetails(eventID);
      model.eventRestrictions = getEventRestrictions(eventID);
      model.usersAttending = getUsersAttending(eventID);
      return View(model);
     }
     else
     {
      return View();
     }
 }

 [HttpPost]
 public ActionResult EventDetails(int? eventID, int? action)
 {
     EventDetailsViewModel model = new EventDetailsViewModel();

     if (eventID != null)
     {
      model.afterPost = true;
      model.currentAttendance = getCountAttending(eventID);
      model.currentUser = getCurrentUserProfile(User.Identity.Name);
      model.eventDetails = getEventDetails(eventID);
      model.eventDetails["ActionID"] = action.ToString();
      model.eventRestrictions = getEventRestrictions(eventID);
      model.usersAttending = getUsersAttending(eventID);
      return View(model);
     }
     else
     {
      return View();
     }
 }

视图很大,但我会发布相关文章:

@if(User.Identity.IsAuthenticated)
{
 if (!Model.eventDetails["Event_Owned"].Equals("true"))
 {
     <div class="joinEventButton">
      @if(Model.eventDetails["Event_Current"].Equals("true"))
      {
          <form method="post" action="@Url.Action("EventDetails", "Event", new{eventID = Int32.Parse(Model.eventDetails["EventID"]), action = Int32.Parse(Model.eventDetails["CODE_RequestInvitation"])})">
           <input type="submit" value="Request Invitation" class="submitButton submitButton-green"/>
          </form>
      }
      else
      {
          <button class="disabledButton disabledButton-grey">Request Invitation</button>
      }
     </div>
 }

为了更好的衡量,我的路线:

    public static void RegisterRoutes(RouteCollection routes)
    {
        //routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

所以这个想法是基于身份验证以及他们是否拥有他们正在查看的特定事件,他们将在屏幕上显示不同的按钮。这是为了当他们想参加一个活动时,他们可以按下一个按钮来请求邀请。控制器知道这一点,因为 int 值作为 CODE_RequestInvitation 传回。返回的操作始终为空。我不知道出了什么问题。

【问题讨论】:

  • 尝试将 eventID 放在表单的隐藏字段中,然后它可能会被发送出去。
  • 检查 html。表单action 属性的值是多少?

标签: c# asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

您的问题是使用了参数名称action(与action 属性冲突)。将其更改为另一个名称,它将起作用。在更改前后检查 &lt;form&gt; 元素的 html。

您生成表单的代码很糟糕,使用Int32.Parse() 毫无意义。

@using (Html.BeginForm("EventDetails", "Event", { eventID = Model.eventDetails["EventID"], actionID = Model.eventDetails["CODE_RequestInvitation"] }, FormMethod.Post))
{
  <input type="submit" value="Request Invitation" class="submitButton submitButton-green"/>
}

然后发回给

[HttpPost]
public ActionResult EventDetails(int? eventID, int? actionID)

【讨论】:

  • 太棒了!像这样奇怪的错综复杂的事情比我敢说的更让我着迷。非常感谢您再次回复(您上次帮助了我)。
  • 没问题,请注意修改。始终使用Html.BeginForm 生成表单
  • 另一方面,你用糟糕的表单评论激起了我的好奇心。我仍然只是在学习 C# 和 MVC,我认为如果你能告诉我它有哪些糟糕的部分以便我修复它们会很棒。
  • 只是您尝试手动生成容易出现拼写错误的&lt;form&gt; 元素的事实。 MVC 具有用于生成 html 的出色 html 助手,您应该始终使用 then(并且始终使用强类型助手来呈现控件)。除了它们可以工作并且意味着编写更少的代码这一事实之外,它们还会验证其他内容,例如BeginForm 将验证路由是否与您的路由表匹配并抛出错误不是(手动编码标签不会很明显的事情)。
  • 对于Int32.Parse(),html 中的所有内容都是字符串,因此您所做的只是将值转换为 int,然后浏览器将其直接转换回字符串。出于好奇,为什么eventDetails 属性是字典而不是包含eventID 等属性的模型?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-18
  • 2019-02-04
相关资源
最近更新 更多