0

Wasn't able to find this.. maybe I wasn't looking correctly. But pretty much this is my view:

<div class="jumbotron">
    @foreach (DataRow dr in ds.Tables[0].Rows)
    {
        <ul>
            <li>@dr["Name"].ToString() - @dr["Distance"].ToString()</li>
        </ul>
    }
</div>

I want to create a new page (using c# and MVC) for each of the bullet points with different URL's depending on the name. How can I do that? Do I use ActionLink? Does it have to do with Routing?

Basically let's say you have a list of Restaurants, I want it so that when you click on a restaurant it takes you to a different URL like yourdomain.com/restaurant-name, and then I want to populate that page with some HTML. How do I go about doing that?

1
  • You really should not be consuming DataSets and DataTables from a view. Create a Model and fill it with the proper data. Look into Entity Framework (ORM that makes database to model mapping a breeze). Commented Jul 14, 2015 at 2:55

2 Answers 2

1

The default route takes urls in the format: domain.com/controller/action/id

So if you code up a Controller called RestaurantsController with an Action View then you can pass it the id of the restaurant you want to view... thusly:

Yes, this is VB, I'm sure you can translate

Public Function View(Optional ByVal id As String) As ActionResult

    '  Fetch your restaurant data...
    Dim model As Restaurant = RestaurantRepository.GetRestaurantById(id)

    Return View(model)

End Function  

access the action via: domain.com/Restaurants/View/whatever-your-restaurant-id-is

Then to make links for it in your razor view you can do:

@Html.ActionLink("Link text here", "View", "Restaurants", 
                 htmlAttributes:=Nothing, routeValues:= New With {.id = dr["Name"]})
Sign up to request clarification or add additional context in comments.

4 Comments

sorry to be a noob and thanks for the help! But anyway you could convert that into c#?
@vbaid: nope. I'll leave that for you to do.
Actually, I just have one more question if that's okay @Sam Axe, I followed this and converted it. I then also made a ShowMenu.cshtml view, but when I run the program it gives me an error such as "the view or its master was not found or no view engine supports the searched locations"
I think I forgot the link text parameter earlier.
0

View:

<div class="jumbotron">
    @foreach (DataRow dr in ds.Tables[0].Rows)
    {
        <ul>
            <li>@Html.ActionLink(@dr["Name"].ToString() + "-" + @dr["Distance"].ToString(), "ShowMenu", routeValues: new {id = dr["Name"] })</li>
        </ul>
    }
</div>

Controller:

public ActionResult ShowMenu(string id)
        {
            return View(id);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.