ID值不会传递到不同的操作方法 [英] ID value not passed to different action method

查看:84
本文介绍了ID值不会传递到不同的操作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,有两个控制器,即家庭控制器和酒店controller.I已经使用视图模型结合两个模型classes.Bellow我有我添加的控制器。

In my project, there are two controllers namely Home controller and Hotel controller.I have used view model to combine two model classes.Bellow I have Add my controllers.

家庭控制器

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);
    }

下面

时,指数view.I的一部分,已在饭店控制器值id传递给详细操作方法。

below is part of Index view.I have passed id value to Details action method in Hotel Controller.

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

但价值没有通过,而不是显示酒店Controller.Since详细操作方法的详细内容我是新来的MVC 4我能不能发现其中的错误。

but value not passed and not display details of Details Action method in Hotel Controller.Since I'm new to mvc 4 I couldn't able to find the mistake.

推荐答案

尝试明确包括附加参数,将重新present你的 htmlAttributes 参数,将匹配<一个href=\"https://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink(v=vs.118).aspx#M:System.Web.Mvc.Html.LinkExtensions.ActionLink%28System.Web.Mvc.HtmlHelper,System.String,System.String,System.String,System.Web.Routing.RouteValueDictionary,System.Collections.Generic.IDictionary%7BSystem.String,System.Object%7D%29\"相对=nofollow>此重载以确保你的对象是沿着正确的传递路径参数:

Try explicitly including an additional null parameter that would represent your htmlAttributes parameter and would match this overload to ensure that your object was properly passed along as route parameters :

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

的ActionLink()助手不包含只接受文字,动作,控制器和RouteValues​​,你所提供的过载,所以这个额外的参数是必要的

The ActionLink() helper doesn't contain an overload that only accepts the Text, Action, Controller and RouteValues, which you have supplied, so this extra parameter is necessary.

这篇关于ID值不会传递到不同的操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆