1

The first view, containing the value i want to pass along looks like this:

@foreach (var item in Model.BlogPosts)
    {
      @item.Id <---This is the id i want to pass along
      <div id="allphotos"><p>Från bildbank</p></div>
    }

This is the Jquery triggered by the #allphotos. I need to have the @item.Id with me in here if possible.

$("#allphotos").click(function () {

    $("<div></div>")
        .addClass("dialog")
        .appendTo("body")
        .dialog({
        close: function () {
            $(this).remove();
        },
        modal: true,
        height: 600,
        width: 700
    })
        .load("/Home/AllPhotos");
});

The Jquery opens up a dialog and in this dialog i need to be able to acess the @item.Id somehow.

Here is the "final"-view where i need to acess the Id:

@model aPhoto_web.Models.AdminPages.AdminViewModel

/* Somewhere here i need to be able to read the @itemId in order to be able to pass it in the actionlink below. */

@foreach (var item in Model.Photographys)
{
    <img id="imga" style="max-width: 100px;" src="@item.ImgUrl" />
    <p>@item.ImgUrl</p>
    @Html.ActionLink("Create New Part", "SetBlogImg", "Home", new {contId = @item.Id, imgurl = @item.ImgUrl}, null)
 }

Is it possible to achieve this somehow? Thanks!

3 Answers 3

2

You can pass the ID as below.

<div data-photoid="@(item.Id)" id="allphotos"><p>Från bildbank</p></div>

.load("/Home/AllPhotos?itemid=" + $(this).data("photoid"));

In your controller change the AllPhotos method as below to get the ID.

public ActionResult AllPhotos(int itemid)

Then when you set the AdminViewModel you can use that value.

Sign up to request clarification or add additional context in comments.

2 Comments

This seems like the way to go, Thanks again! It seems that you and Exception are saving the day. Thanks!
@Exception: It seems both of us are checking MVC related questions.
1

Give <div id="allphotos"> a attribute as :

<div id="allphotos" data-id="@item.Id">

$("#allphotos").click(function () {
      var id=$(this).attr("data-id");   <-----retrieve data-id here as shown

            $("<div></div>")
                .addClass("dialog")
                .appendTo("body")
                .dialog({
                    close: function() { $(this).remove(); },
                    modal: true,
                    height: 600,
                    width: 700
                })
                .load("/Home/AllPhotos?itemid=" + id);

        });

Your Action will look like this :

public ActionResult AllPhotos(int itemid){....}

3 Comments

Thank you, i will try this now. But i cant see that the value is getting passed to the new view?
@user2915962...i think my answer deserve's a upvote atleast not downvote...haha...
Im sure it does, however, i decided to go with Saranga this time. It seems that the controller gets to do some more work that way. But thanks a lot!
0

Send the Value to a controller method, then with your 'new' view set @Value== Html.Action(callMethodHere)

There is plenty of documentation about using MVC in its correct way (as view should only be there to VIEW the project, the Controller is there to handle the data!)

2 Comments

This sound very interesting, I would reallt appreciate if you could provide an example of how to send this value(a string) to a controller that hold that value and let me retrieve it later.
codeproject.com/Articles/639709/… <--- This is a great article on it. Pay close attention to the 'return Content(sbInterest.ToString());'

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.