visual studio 2015, resharper, razor and formatting embedded html - asp.net-mvc

I am running visual studio 2015 with resharper with an mvc application. I have a View in which I want to create a set of nested div tags, a la:
<div>
<label>...
<div>
<label>...
<label>...
<label>...
</div>
<div>
<div>
<label>...
<label>...
</div>
<label>...
<label>...
</div>
</div>
I have code like
<div class="content">
#{
var count = 0;
var depthList = new Stack<int>();
<text>
<div class="NestedSelect">
</text>
foreach (var category in Model.OrderBy(m => m.LeftLink)) {
if (depthList.Count == 0) {
depthList.Push(category.Depth);
<text>
<label>
<input type="radio" name="categories" value="#category.Id"><span>#category.Name</span>
</label>
</text>
} else if (category.Depth > depthList.Peek()) {
depthList.Push(category.Depth);
<text>
<div>
<label>
<input type="radio" name="categories" value="#category.Id"><span>#category.Name</span>
</label>
</text>
} else if (category.Depth == depthList.Peek()) {
<text>
<label>
<input type="radio" name="categories" value="#category.Id"><span>#category.Name</span>
</label>
</text>
} else {
while (category.Depth < depthList.Peek()) {
depthList.Pop();
//####
<text>
</div>
</text>
}
<text>
<label>
<input type="radio" name="categories" value="#category.Id"><span>#category.Name</span>
</label>
</text>
}
//####
<text>
</div>
</text>
count++;
if (count == 0) {
}
}
}
</div>
EDIT
Notice the wonky <text></div></text> blocks (marked with //####) inside the loop. VS and/or resharper are very put out that my closing div tags do not seem to have mates. The opening <text> tags are highlighted in the editor but the ending </text> tags are not. They have an error underline and the tooltip "End tag is missing matching start tag".
I have tried using the #: constructs but when the code is formatted it puts the #: on a line by itself and then gets confused with the naked html tags.
How does one build nested html structures with this?

The #: construct should be used here as you suggested,but only on the tags that are causing the issue.
try something like this:
<div class="content">
#{
var count = 0;
var depthList = new Stack<int>();
<text>
<div class="NestedSelect">
</text>
foreach (var category in Model.OrderBy(m => m.LeftLink))
{
if (depthList.Count == 0)
{
depthList.Push(category.Depth);
<text>
<label>
<input type="radio" name="categories" value="#category.Id"><span>#category.Name</span>
</label>
</text>
}
else if (category.Depth > depthList.Peek())
{
depthList.Push(category.Depth);
#:<div>
<text>
<label>
<input type="radio" name="categories" value="#category.Id"><span>#category.Name</span>
</label>
</text>
}
else if (category.Depth == depthList.Peek())
{
<text>
<label>
<input type="radio" name="categories" value="#category.Id"><span>#category.Name</span>
</label>
</text>
}
else
{
while (category.Depth < depthList.Peek())
{
depthList.Pop();
#:</div>
}
<text>
<label>
<input type="radio" name="categories" value="#category.Id"><span>#category.Name</span>
</label>
</text>
}
#:</div>
count++;
if (count == 0)
{
}
}
}

You should use #: construct if you are not obsessed with formatting.
#: </div>
If you are, consider using WriteLiteral :
WriteLiteral("</div>");

Related

Edit action has not been hitting while I push the submit button

I have an edit button in each row of my Datatable. I have two actions for editing. One for Getting data in a Datatable and the other one for posting my information. The code behind my Edit button in the my Home Index is:
{
"data": "Id",
"render": function (data, type, full, meta) {
return `<div class="text-center"> <a class="btn btn-info"
href="/Home/EditGet/` + data + `" >Edit</a> </div> `;
}
and my home controller methods are:
/// Get Edit
[HttpGet]
[Route("{Id}")]
public IActionResult EditGet(int? id)
{
if (id == null || id == 0)
{
return NotFound();
}
var obj = _sv.OpenRecord(id);
if (obj == null)
{
return NotFound();
}
return View("EditGet", obj);
}
/// Post Edit
[HttpPost]
public IActionResult EditPost(SalesVeiwModel sales)
{
if (ModelState.IsValid)
{
var res= _sv.Update(sales.Comment);
if (res==null )
{
return Json(data: "Not found");
}
return RedirectToAction("EditGet");
}
return Json(data: "Is not valid");
}
And finally my EditGet view is like bellow:
<form id="contact-form" method="post" asp-controller="Home" asp-
action="EditPost" role="form" >
<input asp-for="Id" hidden />
<div class="form-group">
<label>Invoice Nomber</label>
<input id="form_IBNo" type="text" class="form-control" disabled asp-for="IBNo">
</div>
.
.
.
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Confirm" asp-
controller="Home" asp-action="EditGet">
</form>
You should have two buttons,one call EditGet,one call EditPost,here is a demo:
<form id="contact-form" method="post" asp-controller="Home" asp-
action="EditPost" role="form" >
<input asp-for="Id" hidden />
<div class="form-group">
<label>Invoice Nomber</label>
<input id="form_IBNo" type="text" class="form-control" disabled asp-for="IBNo">
</div>
.
.
.
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Confirm">
<a class="btn btn-success btn-send" value="Confirm" asp-controller="Home" asp-action="EditGet" asp-route-id="1">EditGet</a>
</div>
</form>

Show or Hide History in MVC

I am working on a registration form. I want to show and hide my users past registrations using a button.The button should only show or hide registrations that are gone not the upcoming ones This is what I have so far. Pleasssseeee Help.
<div class="Table01">
<button id="older">Show Registration History</button>
#foreach (var sm in Model)
{
var tmp = #sm.SeminarNm.Replace("&", "and");
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 well table-item" align="left" data-toggle="tooltip" data-eventtype="#tmp" data-placement="top" title="#tmp">
<span class="sortName mid-weight"> #sm.SeminarNm</span>
<span class="sortDate alert-info">(ON #string.Format("{0:yyyy-MM-dd}", #sm.SessionStartDT) IN #sm.SessionCityNm)</span>
<div class="row " style="margin-top:10px">
#if (#sm.IsEditable == "Y")
{
using (Html.BeginForm("EditRegister", "App", FormMethod.Post, new { onclick = "showPageLoadingSpinner()" }))
{ #Html.AntiForgeryToken()
<div class="col-xs-12 col-md-6 col-lg-6">
<input class="btn btn-success " name="submitButton" type="submit" value="Edit" />
<input type="hidden" value="#sm.RegistrantSeq" name="hiddenseq" />
<input type="hidden" value="0" name="cntView" />
<input type="hidden" value="EditRegister" name="cntStage" />
</div>
}
}
#using (Html.BeginForm("ViewRegister", "App", FormMethod.Post))
{ #Html.AntiForgeryToken()
<div class="col-xs-12 col-md-6 col-lg-6 col">
<input class="btn btn-info" name="submitButton" type="submit" value="View" />
<input type="hidden" value="#sm.RegistrantSeq" name="hiddenseq" />
<input type="hidden" value="ViewRegister" name="cntStage" />
</div>
}
//
</div>
}
</div>
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
<script>
var $btns = $('.btn').click(function () {
if (this.id == 'older') {
$('#child > div').toggle(450);
}
$btns.removeClass('active');
$(this).addClass('active');
})
</script>
My Program Pic
I dont know if I need some sorting javascript function to display only those sessions that are in the past. Nothing seems to be working.
Assuming old registrations are any item with SessionStartDT value earlier than current date, you can set an html data attribute on each item's container div indicating whether it is an old item or new item and when user clicks the hide/show button, toggle the visibility of these items.
#foreach (var sm in Model)
{
<div data-old="#(p.SessionStartDT.Date < DateTime.Today.Date)">
<!-- your existing code for rendering each item goes here -->
</div>
}
And in the javascript part, when the button is clicked, make select the elements who's data-old attribute value is True (which we set via our C# expression which results in a boolean value) and toggle the visibility.
$(document).ready(function() {
$("#older").click(function(e) {
e.preventDefault();
$("[data-old='True']").toggle();
});
});

object reference not set to an instance of an object render partial view [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 6 years ago.
Below is my partial view call:
<div id="div-reviewgrid">
#Html.Partial("_ReviewList", ViewBag.RewiewLists as List<Pollit.Data.Review>)
</div>
this is my partial view inside shared folder under Partial:
#using Pollit.Data;
#{
List<Review> reviewList = ViewBag.RewiewLists;
}
<br /><br />
#if (reviewList != null)
{
foreach (var review in reviewList)
{
<div class="col-lg-12">
<div id="#review.Id">
<div class="col-lg-1 col-md-1 col-xs-12 col-sm-12">
#if (review.Rating != null)
{
<img src="/images/ratings/#review.Rating.Number-bars.png" class="pull-left" style="max-height: 50px; max-width: 50px; margin-left: 30px" />
}
</div>
<div class="col-lg-11 col-md-11 col-xs-12 col-sm-12">
<span>#review.Creator.Name #String.Format("{0:d}", review.Created)</span>
<br />
<h4 class="custum-memphisfontmediumitalic ">#review.Content</h4>
#*<input type="submit" value="Like" class="btn btn-info" />
<input type="submit" id="click" value="Comment" class="btn btn-info" />
<input type="text" id="comment" placeholder="Enter your comment" class="form-control" />*#
</div>
</div>
<br />
#if (Request.IsAuthenticated == true)
{
if (review.Replys != null)
{
foreach (var reply in review.Replys.OrderBy(c => c.Created))
{
<div id="reply_#reply.ReplyID" class="col-lg-8 col-md-10 col-xs-12 col-sm-12 col-lg-offset-1 ">
<span>Reply By:#reply.Creator.Name #String.Format("{0:d}", #reply.Created)</span>
<br />
<h4 class="custum-memphisfontmediumitalic">#reply.ReplyContent</h4>
</div>
}
}
<div class="col-lg-8 col-md-10 col-xs-12 col-sm-12 col-lg-offset-1 form-group ">
<input type="text" id="txtReply_#review.Id" placeholder="Enter your reply" class="form-control" />
</div>
<div class="col-lg-2 form-group"><div class="demo">
Reply
<img src="/Images/Comment rate up.png" class="img-responsive" width="40px" ><img src="/Images/Comment rate down.png" class="img-responsive" width="40px" ></div>
</div>
}
</div>
}
}
<br /><br />
Above is my partial view code so guys let me know if I am doing something wrong. Please help me. Thanks in advance for help
when you are calling your partial view the way you have written its wrong. each time you call it the new model is generating and your model goes empty`
#Html.Partial("_ReviewList", ViewBag.RewiewLists as List<Pollit.Data.Review>)
write Model instead of ViewBag.RewiewLists as List<Pollit.Data.Review>

Parser Error in this View

Just melt my brain out trying to figure out why I'm getting Parser Error with this code:
#foreach(var news in Model.News)
{
if (news.Order > 0)
{
<text>
<div class="colPrincipal newsTop newsBlue">
<article>
<a href="news.html">
</text>
if (news.LayoutExibitionMainImage)
{
<text>
<span class="newsImg">
<img src="~/Content/img/news/not2.jpg" alt="" title="" />
</span>
</text>
}
<text>
<hgroup>
</text>
if (news.LayoutExibitionMainTitle)
{
<text>
<h1>
#Html.Raw(news.Title)
</h1>
</text>
}
if (news.LayoutExibitionMainSummary)
{
<text>
<h2>
#Html.Raw(news.Summary)
</h2>
</text>
}
<text>
</hgroup>
<span class="btnMais">Ler este artigo</span>
</a>
</article>
</div>
</text>
}
}
Its throwing this error on my last <text> tag:
Is there a way to fix this or a better way to print that HTML code? I hope this is not a silly question because is wierd to me.
Try this, i think you have an issue with all those text tags. Also text is not allowed in the hgroup tags.
#foreach(var news in Model.News)
{
#if (news.Order > 0)
{
<div class="colPrincipal newsTop newsBlue">
<article>
<a href="news.html">
#if (news.LayoutExibitionMainImage)
{
<span class="newsImg">
<img src="~/Content/img/news/not2.jpg" alt="" title="" />
</span>
}
<hgroup>
#if (news.LayoutExibitionMainTitle)
{
<h1>
#Html.Raw(news.Title)
</h1>
}
#if (news.LayoutExibitionMainSummary)
{
<h2>
#Html.Raw(news.Summary)
</h2>
}
</hgroup>
<span class="btnMais">Ler este artigo</span>
</a>
</article>
</div>
}
}

How can I show a validation div when data-validation triggers

Instead of styling the data-valmsg-summary produced by Html.ValidationSummary() in a custom way, I would like to just show the box with the twitter bootstrap style applied to it whenever the field validation fails. How would I go about doing this? Currently my markup looks like this:
..
<script src="#Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<div class="container">
<div class="row-fluid">
<br />
<br />
<br />
<br />
</div>
<div class="row-fluid">
<form class="navbar-form pull-right" action="/Login?ReturnUrl=%2F" method="post">
<h3 class="modal-header">Please sign in</h3>
<input data-val="true" data-val-required="The Username field is required." id="Username" name="Username" type="text" class="input-large" placeholder="Username">
<input data-val="true" data-val-required="The Password field is required." id="Password" name="Password" type="password" class="input-large" placeholder="Password">
<label class="checkbox">
<input type="checkbox" value="remember-me">
Remember me
</label>
<button type="submit" class="btn btn-danger">Sign in</button>
<br />
<br/>
<div data-valmsg-summary="true" class="alert alert-danger alert-block" id="formval" >
<span class="close pull-right" data-dismiss="alert">×</span>
<strong>Ooops!</strong> You seem to be missing something:
<ul>
<li style="display: none"></li>
</ul>
</div>
</form>
</div>
</div>
I've tried adding the style="display: none" to my div, but that does not seem to do the trick either.
I was looking for something else and stumbled on this. Thought I would post the answer for the next person since I am 5 months late. Add to your document ready.
//I dont want to validate my ajax forms take that if statement out if you want 2.
if ($('form:not([data-ajax="true"])').length != 0) {
var settings = $.data($('form:not([data-ajax="true"])')[0], 'validator').settings;
settings.submitHandler = function (form) {
//success
form.submit();
};
}
$("form").bind("invalid-form.validate", function (form, validator) {
var errors = validator.numberOfInvalids();
var message = "<ul>";
//loop thru the errors
for (var x = 0; x < validator.errorList.length; x++) {
var $group = $(validator.errorList[x].element).parent().parent(); //gets bootstrap class of form-group
var $element = $(validator.errorList[x].element); // gets the element to validate
var elementMessage = validator.errorList[x].message; // gets the message
$group.addClass("has-error"); // adds the bootstrap class has-error to the group
$element.popover({ content: elementMessage, placement: "right" }).popover("show"); // adds a popover
message += "<li>" + elementMessage + "</li>"; //appends message to list
}
message += "</ul>";
// Function I have to add alert to the page, but basically you can do whatever you want with the message now.
RegisterError("There was some errors with your submission!", message, false);
});

Resources