如何将JavaScript int变量传递到ASP.NET MVC部分视图中?
在下面的代码中,我试图迭代如何将JavaScript int变量传递到ASP.NET MVC部分视图中?,javascript,c#,asp.net-mvc,Javascript,C#,Asp.net Mvc,在下面的代码中,我试图迭代geoMarkers,并为每一个创建一个标记。这里的代码行需要一些帮助: 内容:@Html.Partial(“~/Views/Search/\u MyPartialView.cshtml”,Model.ElementAt('+i+')) 在上面的这一行中,我试图将JavaScript变量I传递到ASP.NET MVC调用中,我通过搜索知道这是不可能的,除非JavaScript变量可以转换为文本。在本例中,我想将I转换为int,但我正在努力解决语法问题如何将I传递到@Ht
geoMarkers
,并为每一个创建一个标记。这里的代码行需要一些帮助:
内容:@Html.Partial(“~/Views/Search/\u MyPartialView.cshtml”,Model.ElementAt('+i+'))
在上面的这一行中,我试图将JavaScript变量I
传递到ASP.NET MVC调用中,我通过搜索知道这是不可能的,除非JavaScript变量可以转换为文本。在本例中,我想将I
转换为int,但我正在努力解决语法问题如何将I
传递到@Html.Partial
调用中?注意\u MyPartialView.cshtml
采用@model MyMarkerObject
谢谢大家!
@model List<MyMarkerObject>
@section scripts {
<section class="scripts">
<script type="text/javascript">
var geoMarkers = @Html.Raw(Json.Encode(Model));
// Using the JQuery "each" selector to iterate through the JSON list and drop marker pins
$.each(geoMarkers, function (i, item) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(item.GeoLatitude[0], item.GeoLongitude[0]),
map: map,
title: item.GeoFormattedAddress[0],
});
///////////////// SET ICON TYPE ////////////////////////
var thisIcon;
if (item.Source.includes("parking")) {
thisIcon = icons.parking.icon;
} else if (item.Source.includes("hotel")) {
thisIcon = icons.hotel.icon;
} else if (item.Source.includes("restaurant")) {
thisIcon = icons.restaurant.icon;
}
// Use a different color depending on the source
marker.setIcon(thisIcon);
///////////////// CREATE MAP EVENTS ////////////////////
// put in some information about each json object - in this case, the opening hours.
var infowindow = new google.maps.InfoWindow({
///////////////////////////////////////////////////
// HELP PLEASE HERE!
///////////////////////////////////////////////////
content: `@Html.Partial("~/Views/Search/_MyPartialView.cshtml", Model.ElementAt(' + i + '))`
});
// finally hook up an "OnClick" listener to the map so it pops up out info-window when the marker-pin is clicked!
google.maps.event.addListener(marker, 'click', function () {
infowindow.open(map, marker);
});
})
不如将变量“i”作为查询参数传递,让控制器根据需要解释并使用它 大概是这样的:
@Html.Partial("~/Views/Search/_MyPartialView.cshtml?elementNo=" + i)
您也可以传递模型MyMarkerObject,然后根据需要对其进行处理。当视图加载时,将首先加载所有服务器端标记(Razor),然后执行javascript代码,如果在加载代码后需要从JS调用服务器端代码,则在本例中需要使用异步Ajax调用 这是代码的示例 控制器
[HttpPost]
public ActionResult MyPartialView( MyMarkerObject myMarkerObject )
{
return PartialView("~/Views/Search/_MyPartialView.cshtml", myMarkerObject );
}
//Javascript
$.ajax({
url: '@Url.Action("Search","MyPartialView")',
type: 'POST',
data: JSON.stringify(item),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
error: function (xhr) {
alert('Error: ' + xhr.statusText);
},
success: function (result) {
var infowindow = new google.maps.InfoWindow({
content: result
});
},
async: true,
processData: false
});
恐怕这是不可能的。Javascript在服务器上运行的
Html.Partial
之后在浏览器上运行。由\u MyPartialView.cshtml
生成的Html是什么?只需将InfoWindow
的数据包含在model@StephenMuecke的确,我可以只包含数据,因为\u MyPartialView.cshtml
相当简单。但是,它在整个项目中被多个地方使用,我尽量不复制/粘贴。@StephenMuecke但如果这确实是最好的方法,那么我会这样做。除非我知道\u MyPartialView.cshtml
是什么:)并且你只需要在视图中包含一次,在一个隐藏元素中,然后.clone()
it,将其添加到DOM,并更新其元素,这是不可能的(i
是一个javascript变量,当服务器端执行@Html.Partial()
代码时,它甚至不存在),您能帮我进一步了解语法吗?我需要像这样在整个调用周围打勾,@Html….
,因为这会返回一个需要字符串化的多行。使用答案中的语法会出现错误“变量“i”在当前上下文中不存在”这是否可能是其他方式?AJAX?这与我需要的非常接近,只是\u MyPartialView.cshtml
采用@model MyMarkerObject
而不是id
。我是否需要将当前的MyMarkerObject
转换为JSON字符串,将其传递到MyPartialView()
方法,然后将其更改回对象,然后将其传递到return PartialView()
?这似乎很奇怪。有更好的方法吗?检查我的编辑我没有测试过这一点,但我希望它接近答案。另一种方法可能是将MyMarkerObject列表传递到部分视图,然后将每个结果添加到隐藏字段中,并使用唯一id连接索引,并考虑编码/解码html
[HttpPost]
public ActionResult MyPartialView( MyMarkerObject myMarkerObject )
{
return PartialView("~/Views/Search/_MyPartialView.cshtml", myMarkerObject );
}
//Javascript
$.ajax({
url: '@Url.Action("Search","MyPartialView")',
type: 'POST',
data: JSON.stringify(item),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
error: function (xhr) {
alert('Error: ' + xhr.statusText);
},
success: function (result) {
var infowindow = new google.maps.InfoWindow({
content: result
});
},
async: true,
processData: false
});