我做了ActionLink,它打开了PartialView,但是我需要在这个partialView中输入被点击的elemnt的ID。我怎么能这样做?
<script type="text/javascript">
//Below code will clear cache
$.ajaxSetup({ cache: false });
//Below code will run the JQuery when document is ready.
$(document).ready(function ()
{//if you see that code in the partial view our submit button
//is defined as .close class, so below code says on document ready
//append click event to this button
$(".close").live("click", function (e) {
//prevent default action on the button click
e.preventDefault();
//get value of name textbox id=Namecontact
//put in variable name
// HERE I WANT to get ID (see code below)
var t = $("#idtable").val();
var name = $("#Namecontact").val();
//get value of email textbox id=emailcontact
//put in variable email
var email = $("#emailcontact").val();
//get value of message textarea id=messagecontact
//put it in variable message
//note since messagecontact is a textarea
//we need to use .text() rather than .val()
var message = $("#messagecontact").text();
//Start ajax call
$.ajax({
//define method for sending data to controller
type: "POST",
//define controller URL
url: "/Home/ContactUs",
//define data to send to controller
//if you remember our controller accepts
//3 parameters name, email and message
data: { "name": name, "email": email, "message": message },
//create a success function
//this method is executed if controller
//defined by the URL is found
//and it accepts the data sent
//control comes to this point if the controller
//is executed. So data in function(data)
//contains the Json data we sent
//from the controller
success: function (data) {
//Now in the div with id result defined in
//index page previously we will put in the
//data. As you can see Json data is very
//easy way of returning to ajax call because
//now you can simply say data(dot)variable sent
//and you will get the returned value.
$("#result").html("<ul><li>Name: " + data.nameret + "</li><li>Email: " + data.emailret + "</li><li>Message: " + data.messageret + "</li><li>Table: "+data.TableNumber+"</li></ul>");
//now close the modal dialog we opened
$(".dialog").dialog("close");
},
//control comes to this function if ajax doesn't
//find the controller or controller doesn't accepts
//the data sent or controller throws exception
error: function (data) {
//If it happens alert user of the error
alert("There was error processing this");
//close the modal dialog
$(this).closest(".dialog").dialog("close");
}
});
});
//Below code will append a click event to the class openDialog
//which we assigned to the anchor previously
$(".openDialog").live("click", function (e) {
var TableId = window.event.srcElement.id;
//Below code will prevent default action from occuring
//when openDialog anchor is clicked
e.preventDefault();
// Here I want to get Id of elemnt, and save it in elevent value
//idtable
$('#idtable').val('TableId');
//This is the actual implementation of the dailog
$("<div></div>")
//To the div created for dialog we will add class named dialog
//this is done so that we can refer to the dialog later
//we will see this in a short while why it is important
.addClass("dialog")
//add attribute add id attribute
//note id attribute is assigned the same value as data-dialog-id
//attribute in openDialog anchor
.attr("id", $(this).attr("data-dialog-id"))
//below code appends this div to body
.appendTo("body")
//below code describes the attribute for the dialog
.dialog({
//below code assigns title to the dialog
//here we use same title as data-dialog-title attribute
//in openDialog anchor tag
title: $(this).attr("data-dialog-title"),
//this will append code to close the dialog
//and also put close cross(x) in dialog
//we press ESC key in keyboard will also close dialog
close: function () { $(this).remove() },
//below code defines that the dialog is modal dialog
modal: true
})
//below code says take href from openDialog anchor
//which is /Home/ContactUs and load it to the modal dialog
.load(this.href);
});
}
);
或者如果您可以提供其他解决方案,那就太棒了。我打开模态窗口,用户可以放一些信息,然后发送给我。但是我想知道发送它的元素的ID
答案 0 :(得分:1)
您可以通过以下操作链接将您想要的任何内容传递给部分视图:
查看:
@Html.ActionLink(
"test", // linkText
"myAction", // actionName
"MyController", // controllerName
new { // routeValues
myID = Model.someID
},
null // htmlAttributes)
控制器:
public PartialViewResult myAction(int myID)
{
myData=//do whatever with your id
return PartialView(myData);
}
答案 1 :(得分:0)
我在asp.net页面中的代码
@foreach (var item in Model)
{
@Html.ActionLink(" ", "ContactUs", "Home", new { myId = item.IdTable}, new { @class = "openDialog",
data_dialog_id = "emailDialog", value = "1", data_dialog_title = "contactus", @id = "_Logo" + item.IdTable })
}
我的控制器
public ActionResult ContactUs(int myId, string name,string email,string message)
{ /*Your other processing logic will go here*/
return Json(new
{
nameret=name,
emailret=email,
messageret=message
},JsonRequestBehavior.AllowGet);
}