Reputation: 1365
I have a <span>
which the value is depended upon a condition:
@{
if(Session["Status1"]!=null){
<span id="currentStatus">Found</span>
}
else {
<span id="currentStatus">Not Found</span>
}
}
As you can see because of the conditional checking, there will never be 2 <span>
with the same id currentStatus
. However, Visual Studio 2012 (not Community nor Express) is giving me a warning:
Another object on this page already uses ID currentStatus.
How do I get rid of this warning?
Upvotes: 1
Views: 27
Reputation: 24957
As stated by this feedback, this behavior is by design.
Unfortunately the HTML validator is not designed to determine if blocks are being conditionally emitted due to server code. Therefore, it only sees the HTML, not the if statement, and thinks there are two elements with the same ID.
To get rid of this warning completely, you can create a variable and use it within single span
element:
@{
string status = (Session["Status1"] != null) ? "Found" : "Not Found";
}
<span id="currentStatus">@status</span>
Or put conditional check in controller action method instead and assign the result to a viewmodel string property:
// Model
public class ViewModel
{
public string Status { get; set; }
}
// Controller
public ActionResult ActionName()
{
var model = new ViewModel();
model.Status = (Session["Status1"] != null) ? "Found" : "Not Found";
return View(model);
}
// View
@model ViewModel
<span id="currentStatus">@Model.Status</span>
NB: Unfortunately I can't see the warning in VS 2013 and above, hence it possibly only affects certain version of MVC (e.g. MVC 3).
Similar issue:
Give id to html element inside Razor without using class
Upvotes: 1