【问题标题】:ID value not passed to different action methodID 值未传递给不同的操作方法
【发布时间】:2016-06-14 19:11:34
【问题描述】:

在我的项目中,有两个控制器,即家庭控制器和酒店控制器。我使用视图模型来组合两个模型类。下面我添加了我的控制器。

家庭控制器

public ActionResult Index(){
        List<ImageData> details = new List<ImageData>();

        var sp_details = (from s in db.service_provider
                          join p in db.pictures on s.SPID equals p.SPID
                          join c in db.cities on s.City_ID equals c.City_ID
                          select new { s.SPID, s.Sp_name, s.Sp_rate, s.service_type, c.Cityname, p.pic }).OrderByDescending(s => s.Sp_rate).Where(p => p.service_type == "Restaurant").Take(3).ToList();

        foreach (var item in sp_details)
        {
            ImageData SpView = new ImageData(); // ViewModel
            SpView.SPID = item.SPID;
            SpView.Sp_name = item.Sp_name;
            SpView.Cityname = item.Cityname;
            SpView.Sp_rate = item.Sp_rate;
            SpView.pic = item.pic;

            details.Add(SpView);
        }
        return View(details);
    }

酒店控制器

public ActionResult Details(int id = 0)
    {
        List<ImageData> details = new List<ImageData>();

        var sp_details = (from s in db.service_provider
                          join p in db.pictures on s.SPID equals p.SPID
                          join c in db.cities on s.City_ID equals c.City_ID
                          where s.SPID == id
                          select new ImageData()
                          {
                              Sp_name = s.Sp_name,
                              Sp_location = s.Sp_location,
                              Cityname = c.Cityname,
                              service_type = s.service_type,
                              Sp_description = s.Sp_description,
                              Sp_rate = s.Sp_rate,
                              Sp_web = s.Sp_web,
                              Cnt_wh = s.Cnt_wh,
                              pic = p.pic
                          });



        if (details == null)
        {
            return HttpNotFound();
        }

        return View(sp_details);
    }

下面是索引视图的一部分。我已将 id 值传递给酒店控制器中的 Details 操作方法。

 <p class="name">@Html.ActionLink(item.Sp_name, "Details","Hotel", new { id = item.SPID })</p>

但是值没有传递并且没有显示酒店控制器中详细信息操作方法的详细信息。由于我是 mvc 4 的新手,所以我无法找到错误。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    尝试显式包含一个额外的 null 参数,该参数将代表您的 htmlAttributes 参数并匹配 this overload 以确保您的对象作为路由参数正确传递:

    @Html.ActionLink(item.Sp_name, "Details","Hotel", new { id = item.SPID }, null)
    

    ActionLink() 帮助器不包含只接受您提供的 Text、Action、Controller 和 RouteValues 的重载,因此这个额外的参数是必要的。

    【讨论】:

    • 只是想知道,如果OP的Details ActionResult的参数已经初始化了..public ActionResult Details(int id = 0),他在ActionLink中的routeValue不会毫无意义吗?还是 OP 的 routeValue override int id =0?
    • int id = 0 表示默认参数的值为 0。因此,如果提供了 id 值,它将覆盖它,如果没有,仍将使用 0。
    • 啊,所以这是一个可选参数
    猜你喜欢
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    相关资源
    最近更新 更多