ID值未传递给不同的操作方法

时间:2016-06-14 19:11:35

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

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

家庭控制器

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);
    }
下面的

是Index视图的一部分。我已将id值传递给Hotel Controller中的Details action方法。

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

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

1 个答案:

答案 0 :(得分:0)

尝试明确添加一个代表null参数的htmlAttributes参数并与this overload匹配,以确保您的对象作为路由参数正确传递:

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

ActionLink()帮助程序不包含只接受您提供的Text,Action,Controller和RouteValues的重载,因此需要此额外参数。