Update record ASP.NET MVC - asp.net

I have a few problems and this is my source code:
Controller:
public ActionResult Index()
{
List<Order> list = new List<Order>();
for (int i = 0; i < 20; i++)
{
list.Add(new Order());
}
list = DatabaseService.DBGetList<Order>("Order");
List<Order> orders = new List<Order>();
for (int i = 0; i < list.Count(); i++)
{
if (list[i].numberOfCompleted == 0)
{
orders.Add(new Order() { id = list[i].id, numberOfCompleted = list[i].numberOfCompleted, customerName = list[i].customerName, foods = list[i].foods });
}
}
return View(orders);
}
Order.cs
public List<Food> foods { get; set; }
public string customerName { get; set; }
public int id { get; set; }
public long totalCost = 0;
public int numberOfCompleted { get; set; }
View
#foreach (var item in Model)
{
<!--Display attributes-->
<!--Display Complete button-->
<div class="media-container-column col-12 col-lg-3 col-md-4">
<div class="mbr-section-btn align-right py-4">
<button class="btn btn-primary display-4 remove"
data-toggle="modal"
data-target="#exampleModal"
type="submit"
>Complete</button>
</div>
</div>
</div>
</div>
</section>
}
I want when clicking the Complete button, the numberOfComplete attribute will be assigned with food.Count(). What do I have to do to achieve this?

First you create a post form in view page of index action. Change this to your index.cshtml file.
#foreach (var item in Model)
{
<!--Display attributes-->
<!--Display Complete button-->
<div class="media-container-column col-12 col-lg-3 col-md-4">
#Html.BeginForm("Index","Controller",FormMethod.post)
{
<input type="text" name="id" display="none" value="#item.id"/>
<div class="mbr-section-btn align-right py-4">
<button class="btn btn-primary display-4 remove" data-toggle="modal" data-
target="#exampleModal" type="submit">Complete</button>
</div>
}
</div>
}
Then create post action method of index.
[HttpPost]
public ActionResult Index(Order obj)
{
Order order=Order().Find(x=>x.id=obj.id);
order.numberOfComplete = order.food.count();
return view();
}

Related

blazor dynamically change css on mouseover when multible divs are displayed

#foreach (var item in RootOfJson.results)
{
<div class="card" #onmouseout="#OnMouseOut" #onmouseover="#OnMouseOver">
<img class="#Imgdisplay" src=#item.image_url alt="...">
</div>
}
#code {
bool _IsHovering = false;
[Parameter]
public string Imgdisplay { get; set; }
protected void OnMouseOver(MouseEventArgs mouseEvent)
{
if (!_IsHovering)
{
_IsHovering = true;
Imgdisplay = "d-none";
StateHasChanged();
}
}
protected void OnMouseOut(MouseEventArgs mouseEvent)
{
Imgdisplay = String.Empty;
_IsHovering = false;
StateHasChanged();
}
Above code changes the css for all divs, can you assist.. i would like to change the class to d-none on the specific div that my mouse event occurs.

How to change dynamically a blazor component

I need to change the items from a list, the list is a component that shows numbers, but its numbers depend from other component named component2, and if component2 change her value component1 should reset and change to the new numbers. component2 is an other list.
this is component1 :
<div>
<div class="d-flex listbox-box" #onclick="DropDown" style="height:#(height)px !important; width:#(width)px !important;padding:0px;">
#if (ItemIsSelected)
{
<IterationItem height="#height" width="#width" _Iteration="Iteration[SelectedIndex]" />
}else{
<div class="align-self-center" style="width:100%;">
<div class="float-end">
<div class=" icon-container" style="transform: rotate(#(rotation)deg) !important;">
<i class="bi bi-chevron-compact-down icon-listbox"/>
</div>
</div>
</div>
}
</div>
#if (ShowDropDown && Iteration != null)
{
<div class="example dropdown-listbox" style="width:#(width)px !important;height:#(height * 3)px !important">
#for (int i = 0; i < Iteration.Length-1; i++)
{
var index = i;
<div id="#i" value="#SelectedIndex" #onclick='eventargs => HandleValueChange(eventargs,index)'>
<IterationItem height="#height" width="#width" _Iteration="Iteration[i]" />
</div>
<hr class="listbox-item-separator"/>
}
</div>
}
</div>
#code {
int rotation = 0;
[Parameter] public int SelectedIndex { get; set; } = -1;
[Parameter] public bool ShowDropDown { get; set; } = false;
[Parameter] public bool ItemIsSelected { get; set; } = false;
[Parameter] public Iteration[] Iteration { get; set; }
public int height { get; set; } = 40;
public int width { get; set; } = 120;
private void DropDown()
{
ShowDropDown = ShowDropDown ? false : true;
if (ShowDropDown)
{
rotation = 180;
}
else
{
rotation = 0;
}
}
protected void HandleValueChange(MouseEventArgs evt, int index)
{
SelectedIndex = index;
DropDown();
ItemIsSelected = true;
}
}
and this is the event that make swap the numbers:
void ClickHandler(int num)
{
_modelValue = num;
_selectedModel =
XqueryFunctions.CreatrModelById(_modelValue,"");
}
and this is how im trying to change it right now :
<ListBoxIteration Iteration="#_selectedModel.Iterations" />
#code {
private string ProyectName { get; set; } = "Fred";
int _modelValue;
Model _selectedModel = new ();
void ClickHandler(int num)
{
_modelValue = num;
_selectedModel =
XqueryFunctions.CreatrModelById(_modelValue,"");
}
}
This code works, i had other problem. I should remove this post ?

How To List Posts From Newest To Oldest In ASP.NET MVC5

I have a web app where users can posts items on the home page. Currently, the items on the home page are listed from oldest date to newest. This is not good because the users want to see the most recent posts when they visit the home page.
I have a DatePosted property in my post model that I use to track the time and date of a posting. How can I sort by Newest to Oldest using that property?
Here is my Action that takes users to the home page where they can see posts:
// GET: Posts
public ActionResult Index()
{
var posts = db.Posts.ToList();
return View(posts);
}
The View:
#model IEnumerable<DothanTrader.Models.Post>
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#{
int i = 5;
}
<div class="row">
#foreach (var item in Model)
{
if (i == 5)
{
#:</div><div class="row">
i = 1;
}
<div class="col-md-3">
<div class="panel panel-default">
<div class="panel-heading">
#Html.ActionLink(#item.Title, "Details", "Post", new { id = item.PostId }, null)
</div>
<div class="panel-body" style="min-height: 200px; max-height: 200px; ">
<img src="#Url.Content("~/Content/images/" + System.IO.Path.GetFileName(item.Image))" alt="" class="img-responsive" />
</div>
<div class="panel-footer">
<span class="glyphicon glyphicon-eye-open"> #item.Views</span> | <span class="glyphicon glyphicon-usd"> #item.Price</span>
<div class="pull-right">
<span>#item.DatePosted.Value.ToShortDateString()</span>
</div>
</div>
</div>
</div>
{ i++; }
}
</div>
The Model (just in case you need it):
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public DateTime? DatePosted { get; set; }
public string Image { get; set; }
public float Price { get; set; }
public int Views { get; set; }
public int Likes { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public string ApplicationUserId { get; set; }
}
Use db.Posts.OrderByDescending(p => p.DatePosted).ToList()

Bootstrap MVC 4 TextBoxFor Required indicator

I am totally confused. I have 6 inputs on a page for a signup page. Three of the boxes have the required indicator in the right side of the box the other three do not. All 6 fields are required in code.
I have looked through the code and either MVC is adding a background image or Bootstrap is.
Here is the Razor code I am using.
<div class="row ">
<div class="col-md-6">
<div class="row">
#Html.TextBoxFor(m => m.currentUser.UserName, new { #class = "form-control", placeholder = "user name", tabindex = 1 })
</div>
<div class="row">
#Html.PasswordFor(m => m.currentUser.Password, new { #class = "form-control", placeholder = "password", tabindex = 3 })
</div>
<div class="row">
#Html.TextBoxFor(m => m.currentUser.LastName, new { #class = "form-control", placeholder = "last name", tabindex = 6 })
</div>
</div>
<div class="col-md-6">
<div class="row">
#Html.TextBoxFor(m => m.currentUser.EmailAddress, new { #class = "form-control", placeholder = "email address", tabindex = 2 })
</div>
<div class="row">
#Html.PasswordFor(m => m.confrimPassword, new { #class = "form-control", placeholder = "confirm password", tabindex = 4 })
</div>
<div class="row">
#Html.TextBoxFor(m => m.currentUser.FirstName, new { #class = "form-control", placeholder = "first name", tabindex = 5 })
</div>
</div>
</div>
I dont have much experience with Bootstrap so I am not sure if that is what is causing the issue or if MVC is.
Any help is appreciated.
Here is the model class:
public class User
{
public int ID { get; set; }
[Required(ErrorMessage = "Please enter a User Name.")]
public string UserName { get; set; }
[Required(ErrorMessage = "Please enter a password")]
public string Password { get; set; }
[Required(ErrorMessage = "Please enter your first name.")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Please enter your last name.")]
public string LastName { get; set; }
[Required(ErrorMessage = "Please enter an email address.")]
public string EmailAddress { get; set; }
}
here is the generated code for an input that has the indicator:
<input class="form-control" data-val="true" data-val-required="Please enter a password" id="currentUser_Password" name="currentUser.Password" placeholder="password" tabindex="3" type="password" autocomplete="off" keyev="true" clickev="true" style="background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAASCAYAAABSO15qAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3QsPDhss3LcOZQAAAU5JREFUOMvdkzFLA0EQhd/bO7iIYmklaCUopLAQA6KNaawt9BeIgnUwLHPJRchfEBR7CyGWgiDY2SlIQBT/gDaCoGDudiy8SLwkBiwz1c7y+GZ25i0wnFEqlSZFZKGdi8iiiOR7aU32QkR2c7ncPcljAARAkgckb8IwrGf1fg/oJ8lRAHkR2VDVmOQ8AKjqY1bMHgCGYXhFchnAg6omJGcBXEZRtNoXYK2dMsaMt1qtD9/3p40x5yS9tHICYF1Vn0mOxXH8Uq/Xb389wff9PQDbQRB0t/QNOiPZ1h4B2MoO0fxnYz8dOOcOVbWhqq8kJzzPa3RAXZIkawCenHMjJN/+GiIqlcoFgKKq3pEMAMwAuCa5VK1W3SAfbAIopum+cy5KzwXn3M5AI6XVYlVt1mq1U8/zTlS1CeC9j2+6o1wuz1lrVzpWXLDWTg3pz/0CQnd2Jos49xUAAAAASUVORK5CYII=); padding-right: 0px; background-attachment: scroll; cursor: auto; background-position: 100% 50%; background-repeat: no-repeat no-repeat;">
The really weird part is I discovered that they show up in Chrome but not Firefox.
#nemesv - you were exactly right. LastPass is one of the extensions I had installed in Chrome. Once I disabled it the icons were removed. Pulled my hair out for days trying to figure that one out.

Parsing error with HTML.Raw and li?

I have the following code in my ASP.NET MVC website :
#foreach (var item in Model.Comments.Where(c=>c.CommentId == Model.CurrentNode))
{
if(Model.Level == 0){
#Html.Raw("<li style=\"border:none;\">")
}
else
{
#Html.Raw("<li>")
}
When trying to visit this page I get the following exception :
Parser Error Message: Encountered end tag "li" with no matching start tag. Are your start/end tags properly balanced?
I have tried alot of diffrent options but can´t get this to work.
EDIT 1:
#if (!Model.CurrentNode.HasValue)
{
#Html.Raw("<div class=\"comment-root\" id=\"comment-root\">")
#*hid911 Tells what voteKey to use for this section*#
<input pid="hidVoteKey" type="hidden" value="1" />
}
<ul class="commentList sub">
#foreach (var item in Model.Comments.Where(c=>c.CommentId == Model.CurrentNode))
{
if(Model.Level == 0){
<li style="border:none;">
}
else
{
<li>
}
#if(!item.Deleted)
{
<div class="commentBack">
#if (Request.IsAuthenticated && uFeed.Handlers.AccountHandler.UserContextInstance.Id != item.CreatedBy.Id)
{
#Html.Partial("VotePartial", item.VoteViewModel)
}
else
{
<div style="float:left; width: 30px; "> </div>
}
<div class ="floatLeft" style="width: 90%">
<div class="CommentHeader" id="commentHeader-#item.Id">
<p class="Timestamp dimText">
#Html.ActionLink(item.CreatedBy.Text, "User/" + item.CreatedBy.Text, "Post")
| #item.VoteBalance poäng |
#Html.GetDisplayDate(item.CreatedDate)
</p>
</div>
<div id="commenttext-#item.Id" class="commentText">
#item.Text
</div>
<div id="comment-#item.Id" class="CommentFooter">
#if (Request.IsAuthenticated)
{
<div class="floatLeft">
#Html.ActionLink("Svara", string.Empty, null, new { id= "respond-" + item.Id, href = "#comment", #class = "respond" })
</div>
}
#if (Request.IsAuthenticated && uFeed.Handlers.AccountHandler.UserContextInstance.Id ==item.CreatedBy.Id)
{
<div class="floatLeft" id="divremove-#item.Id">
- #Html.ActionLink("Editera", string.Empty, null, new { id= "edit-" + item.Id, href = "#comment", #class = "edit" })
- #Html.ActionLink("Ta bort", string.Empty, null, new { id="remove-"+item.Id, #class = "remove"})
</div>
<div class="floatLeft hidden" id="divconfirmremove-#item.Id"> - Ta bort?
#Html.ActionLink("Ja", string.Empty, null, new { id = "confirm-remove-yes-" + item.Id, #class = "confirm-remove-yes" })
/
#Html.ActionLink("Nej", string.Empty, null, new { id = "confirm-remove-no-" + item.Id, #class = "confirm-remove-no" })
</div>
}
#if(Model.Comments.Any(c=>c.CommentId == item.Id))
{
#Html.Raw(" - ")
#Html.ActionLink("Dölj kommentarer", string.Empty, null, new { id = "toggle-" + item.Id, Href = "#togglecomments", #class = "toggle active" })
}
<div style="clear:both;"></div>
</div>
</div>
<div style="clear:both;"></div>
</div>
}
else
{
<div class="commentBack">
<div style="float:left; width: 30px; height: 30px"> </div>
<div style="float:left">
<div class="commentText" ><i>[Borttagen]</i></div>
<div id="comment-#item.Id" class="CommentFooter">
#if(Request.IsAuthenticated)
{
<div class="floatLeft">
#Html.ActionLink("Svara", string.Empty, null, new { id= "respond-" + item.Id, href = "#comment", #class = "respond" })
</div>
}
#if(Model.Comments.Any(c=>c.CommentId == item.Id))
{
#Html.Raw(" - ")
#Html.ActionLink("Dölj kommentarer", string.Empty, null, new { id = "toggle-" + item.Id, Href = "#togglecomments", #class = "toggle active" })
}
</div>
</div>
<div style="clear:both;"></div>
</div>
}
<div id="comments-for-#item.Id" class="CommentContainer">
#Html.Partial("CommentPartial", new uFeed.Views.ViewModels.CommentTreeItemViewModel(item.Id, Model.Comments, Model.Level + 1))
</div>
<div style="clear:both;"></div>
</li>
}
</ul>
You don't need the Html.Raw since you are not actually outputting a code variable that contains HTML. This should work just fine:
#foreach (var item in Model.Comments.Where(c=>c.CommentId == Model.CurrentNode))
{
if(Model.Level == 0){
<li style="border:none;">
}
else
{
<li>
}
You could probably simplify this even further...
#foreach (var item in Model.Comments.Where(c=>c.CommentId == Model.CurrentNode))
{
<li #(Model.Level == 0 ? "style=\"border:none;\"" : "")>

Resources