问题
Using razor it's easy to show the values of a list in html:
<div id="collapse1" style="display:none">
@{
foreach (var i in Model)
{
<text> <strong>::: @i.City, @i.Country :::</strong> </text>
<p style="margin-bottom:0px"></p>
foreach (var h in i.Weather)
{
{
<div id="MyDiv2">
<strong><small>@DateTime.Now.AddDays(count).ToString("dd/MM/yy")</small></strong>
<p style="margin-bottom:0px"></p>
<text><small>Max: @i.MaxTemp.ElementAt(@count)°</small></text>
<p style="margin-bottom:0px"></p>
<text><small>Min: @i.MinTemp.ElementAt(@count)°</small></text>
<span id="MyTest">the min temp: @i.MinTemp.ElementAt(0) </span>
</div>
}
count++;
}
}
}
</div>
Yes, piece of cake. But know imagine that I want to change the text inside the span MyTest after clicking a button so that it has instead: "the max temp: @i.MaxTemp.ElementAt(0)".
My problem is that I would have to mix jQuery (for the button) and razor (to get the maxtemp value). Any help/tip is highly appreciated!
回答1:
<div id="collapse1" style="display:none">
@{
int mycounter=0;
foreach (var i in Model)
{
<text> <strong>::: @i.City, @i.Country :::</strong> </text>
<p style="margin-bottom:0px"></p>
foreach (var h in i.Weather)
{
{
<div id="MyDiv2">
<strong><small>@DateTime.Now.AddDays(count).ToString("dd/MM/yy")</small></strong>
<p style="margin-bottom:0px"></p>
<text><small>Max: @i.MaxTemp.ElementAt(@count)°</small></text>
<p style="margin-bottom:0px"></p>
<text><small>Min: @i.MinTemp.ElementAt(@count)°</small></text>
<span maxtemp=' @i.MaxTemp.ElementAt(0)' id="MyTest">the min temp: @i.MinTemp.ElementAt(0) </span>
</div>
}
count++;
}
}
}
</div>
$('#MyTest').on('click',function(){
$(this).text($(this).attr('maxtemp'));
}):
来源:https://stackoverflow.com/questions/21933961/inserting-model-string-in-span-text-after-button-click