1

I try to call a method from my view page but it is not called. My sample code of Home Controller is

public ActionResult Sample(Guid id)
    {
        List<Images> imgList = ImagesManager.GetAlllImages();
        var bindlist = (from i in imgList where id == i.ImageId select i).ToList();
        return RedirectToAction("Index",bindlist);
    }

And my .cshtml page(view ) code from where I try to call it is

<div class="product">
                            <a href="Sample/?Id=@item.ImageId" class="info">
                                <span class="holder">
                                    <img src="@item.ImagePath" alt="">
                                    <span class="dd" >@item.ImageName</span>
                                    <span class="author">by John Smith</span>

Why the "Sample" method not called.

The error is "resource not found"

Mohd Waseem
  • 186
  • 1
  • 4
  • 19
  • 1
    MVC does not pass variables like that. check this answer maybe that helps you a bit further http://stackoverflow.com/questions/155864/asp-net-mvc-passing-parameters-to-the-controller – bas Mar 10 '13 at 19:28
  • 1
    While it's not ideal MVC form, a query string can be used. It's your route that is wrong. Sample is an action, not a Controller. In this case your route should be "{Controller}/Sample?....". Or better yet, as @bas pointed out, you should use an ActionLink Helper. – Dave Alperovich Mar 10 '13 at 19:32
  • @DaveA Sample is action , but in my previous application I use it same and it works but not now please give some more suggestion thanks – Mohd Waseem Mar 10 '13 at 19:38
  • @MohdWaseem, I can't see how this would work, but really your path should be Controller/Action. What is name of your controller? Use that before your Action and your MVC should find your Action Method – Dave Alperovich Mar 10 '13 at 19:41
  • @DaveA my controller name is Home – Mohd Waseem Mar 10 '13 at 19:43
  • @MohdWaseem, see examples for Home/Sample – Dave Alperovich Mar 10 '13 at 19:49

1 Answers1

1

I think this is what you are looking for

@Html.ActionLink(
    "text for your link", 
    "Sample", // action method
    "Home",   // controller
    new { id = item.ImageId }, // your variable you want to pass
    null) 
bas
  • 13,550
  • 20
  • 69
  • 146
  • now again it is not working :The error message:"The resource cannot be found.Requested URL: /Home/Sample/d4ba72ab-260d-46f7-bc21-36129a2bacdd" – Mohd Waseem Mar 11 '13 at 06:05
  • @MohdWaseem heya sorry was sleeping when you typed all this :). Glad you got it working though. – bas Mar 11 '13 at 08:24