【问题标题】:How to get htmlAction id from my controller?如何从我的控制器获取 htmlAction id?
【发布时间】:2016-03-15 01:56:52
【问题描述】:

url 设法解析 id,例如"mylink/Index/1" 但是它没有在下一行读取我的 id。

  @Html.ActionLink("Submit school rating", "Index", "Review", new { id = Model.School_Code }, new { name = "Index" })

控制器(审查模型)

public ActionResult Index(int? id, ReviewSearch model)
        {
            reviews = revGateway.SelectAll();
            if (id == 0)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            reviews = reviews.Where(s => s.School_Code.Equals(id));

            if (reviews == null)
            {
                return HttpNotFound();
            }
            var pageIndex = model.Page ?? 1;
            model.SearchResults = reviews.ToPagedList(pageIndex, RecordsPerPage);

            return View(model);
        }

查看(学校模型)

  @Html.ActionLink("Submit school rating", "Index", "Review", new { id = Model.School_Code }, new { name = "Index" })

【问题讨论】:

  • 是什么让您认为id 参数未绑定。根据您显示的代码,Model.School_Code 的值是有效的int,那么它将被正确绑定。

标签: c# visual-studio model-view-controller


【解决方案1】:

您的代码没有问题。我能够以同样的方式获得身份证。只要确保 Model.School_Code 是否有价值。尝试先传递一些核心价值,然后检查您是否能够获得该价值。

您也可以像这样使用 RouteData 获得相同的值:

    public ActionResult Test()
    {
        int id;
        int.TryParse(RouteData.Values["id"].ToString(), out id);

        return View();
    }

查看

@Html.ActionLink("提交学校评分", "Test", "Default", new { id = 1 }, new { name = "Index" })

【讨论】:

  • 不。当我将一些值硬编码到 ActionLink 时,它甚至都不起作用。回复“错误请求”
  • 删除所有其他东西,然后尝试像上面显示的那样简单的操作。如果您能够转到控制器,它应该可以工作。调试一下看看能不能
猜你喜欢
  • 1970-01-01
  • 2017-12-05
  • 2021-11-25
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多