ActionResult未被调用

时间:2015-09-28 13:09:13

标签: asp.net asp.net-mvc

我有一个观点,我在这里调用一个ActionResult方法,但在方法中设置一个断点告诉我它没有被调用。

<div>
<ul class="list-group">
    @foreach (var item in Model)
    {
        <li class="list-group-item">
            <h4>Slide ID: @item.SlideId</h4>
            <p><i>Received: @item.TimeStamp</i></p>
            <div class="row">
                <div class="col-md-4">
                    <h4>@Html.ActionLink("View details", "Well", new {slideid = item.SlideId})</h4>
                    <img src="@Url.Action("Index", "Images", new {id = item.SlideId})"/> //This is where I want to call the method                       
                </div>
            </div>
        </li>
    }
</ul>

以下是方法:

public class ImagesController : Controller
{
    // GET: Images
    public ActionResult Index(string id)
    {
        byte[] imageData = new byte[0];
        string cs = "Data Source=" + "some path";

        using (SQLiteConnection con = new SQLiteConnection(cs))
        {
            string stm = "SELECT LastImage FROM Well WHERE SlideId = " + "'" + id + "'";
            con.Open();

            using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
            {
                using (SQLiteDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        imageData = Serialize(rdr["LastImage"]);
                    }

                    rdr.Close();

                }
            }

            con.Close();
        }
        return File(imageData, "image/png");
    }

    public static byte[] Serialize(object obj)
    {
        var binaryFormatter = new BinaryFormatter();
        var ms = new MemoryStream();
        binaryFormatter.Serialize(ms, obj);
        return ms.ToArray();
    }
}

我尝试使用此代码实现的是将数据库中的图像加载到视图中。关于我做错了什么的暗示?

现在使用RouteConfig:

public class RouteConfig
{
    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 }
        );
    }
}

2 个答案:

答案 0 :(得分:0)

当您撰写<img src="@Url.Action("Index", "Images", new {id = item.SlideId})"/>时,您不会调用该操作,但会调用路径网址。结果是一个字符串,例如localhost:8080/images/index/abcd123456,因此,如果要调用该操作,则需要使用@Html.Action("Index", "Images", new {id = item.SlideId})。注意@Html.Action而不是@Url.Action

答案 1 :(得分:0)

我认为不是为每个图像打开和关闭数据库连接,更好的方法是收集所有信息以呈现该页面并将其发送到您发布的视图模型中。说它叫做HomeController的索引动作。它看起来像这样:

public class HomeController : Controller
{
    public ActionResult Index(string id)
    {
        var listOfItems = new List<SomeClass>();

        string cs = "Data Source=" + "some path";
        using (SQLiteConnection con = new SQLiteConnection(cs))
        {
            string stm = "SELECT SlideId, TimeStamp, LastImage FROM Well"; 
            con.Open();

            using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
            {
                using (SQLiteDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        var someItem = new SomeClass()
                        {
                            SlideId = rdr["SlideId"],
                            ImageData = Serialize(rdr["LastImage"]),
                            TimeStamp = rdr["TimeStamp"]
                        };
                        listOfItems.Add(someItem);
                    }

                    rdr.Close();
                }
            }

            con.Close();
        }

        return View(listOfItems);
    }
}

当然,如果项目太多,您应该始终考虑分页并限制列表中的项目数以减少响应时间。