Error with session mvc 4 - c#

When the HomeController start in the Index ActionResults i set a session with the value of 1
Session["login_fail"] = 1;
When the user try to login the sessio variable change to 0 if it fails
Session["login_fail"] = 0;
And if it fails, here what should happen
#using(Html.BeginForm()) {
<fieldset>
<legend>Tbl_Users</legend>
<table>
<tr>
<td>User Name</td>
<td><input type="text" id="username" name="userName" /></td>
<td id="ErrorMessage"></td>
</tr>
<tr>
<td>Password </td>
<td><input type="text" id="pass" name="pass" /></td>
<td id="ErrorMessage"></td>
</tr>
#{
if(Convert.ToInt32(Session["login_fail"]) == 0)
{
<p>User name or password are incorrect. Please, try again.</p> //here should apper the error message if it fails but it doesnt
}
}
</table>
<p>
<input type="submit" onclick="return validate()" value="Login" />
</p>
</fieldset>
Notice that when i submit the form, another action is who recive the values of the post and them check if the user and password are ok, if it doesnt(that i have checked that it does return 0) it will show the error message and will be return to the Index actionresults which is in the same controller as the LoginUser(the one which return 0 or 1)

I found the error. The problem was that when i clicked submit the post values goes to another actionresuts, and then the session's value get 0 if the login failed and them make a redirecttoaction to Index actionresults again and the session's value for some reason, get lost.
The same as if you use a ViewBag, you can not see its value in another view. I dont know why this happen since session should be visible in the whole proyect. But i guest that that was the problem.

It seems like after your LoginUser() method, Index() works again and sets the Session["login_fail"] = 1; again.
You know your [Get] Index() works everytime you load the page. I suggest you to use TempData for passing data throught controller methods.
TempData["login_fail"] = 1;
So you can check if TempData is 1 or 0 in your Index() and set session according to TempData.

Related

OnClick event is now working on asp net core 6

I have a page that receives data from a database, and for each record it creates a row () on my page. Here is the HTML code:
#foreach (var item in #Model.veterinarios)
{
<tr>
<td>
<span class="custom-checkbox">
<input type="checkbox" id="checkbox1" name="options[]" value="1">
<label for="checkbox1"></label>
</span>
</td>
<td>#item.IdVeterinario</td>
<td>#item.Nombre</td>
<td>#item.Apellido</td>
<td>#item.consultorio</td>
<td>
<i class="material-icons" data-toggle="tooltip" title="Edit"></i>
</i>
</td>
</tr>
}
So the last has two elements that open up a div that is at the end of my code. What i would like to do is to SAVE the #item.IdVeterinario on a variable in my controller (model).
Below is the C# code in the HTML view:
#functions
{
public void guardarIdVet(int a)
{
Model.valor= a;
}
}
When the page runs for the first time, that OnClick event fires 3 times. But when i click on it it doesnt do anything.
I ve tried with asp:LinkButton and still does not work. Funny thing is that yesterday it worked and i changed NOTHING

asp-for binding and route-value issues inside loop

I am building a little website where users can manage personal gear, which has a name and mass among other properties. I want to display all of this gear in a table, using input boxes bound to each item so the user can change values if desired, then hit an edit button and update the gear database. I can get my foreach loop to bind all of the right values, but when it routes to the handler, it always sets the EditGearName and EditGearMass value to whatever value is in the first row.
<table>
#foreach (var g in Model.Gears) {
<tr class="list-table-main">
<td><input class="form-control" asp-for="EditGearName" value="#g.GearName" /></td>
<td><input class="form-control" asp-for="EditGearMass" value="#g.Mass" type="number" min="0" /></td>
<td style="width:12.5%"><button type="submit" asp-page-handler="EditGear" asp-route-groupid="#gg.ID">Save</button></td>
</tr>
}
</table>
Here is an example of my handler method called on the button press. The GearBL.EditGear method is code behind that handles saving it to database. I know this works (so long as the right values are passed in)
public IActionResult OnPostEditGroup(int groupid)
{
try
{
base.OnGetParent();
if (string.IsNullOrEmpty(EditGearName))
throw new ValidationException("Gear name must not be empty");
if (EditGearMass < 0)
throw new ValidationException("Gear mass cannot be less than 0");
GearBL.EditGear(groupid, User, EditGearName, EditGearMass, false);
Response.Redirect("GearList");
}
catch (Core.Domain.ValidationException ve)
{
ModelState.AddModelError("Error", ve.Message);
}
return Page();
}
I am new to asp.net/razor so any help would be appreciated. I'm not sure I even have the best way to implement something like either.
it always sets the EditGearName and EditGearMass value to whatever value is in the first row.
You should add the form tag in the foreach loop to enable one submit for one row.
<table>
<form>
#foreach (var g in Model.Gears) {
<tr class="list-table-main">
<td><input class="form-control" asp-for="EditGearName" value="#g.GearName" /></td>
<td><input class="form-control" asp-for="EditGearMass" value="#g.Mass" type="number" min="0" /></td>
<td style="width:12.5%"><button type="submit" asp-page-handler="EditGear" asp-route-groupid="#gg.ID">Save</button></td>
</tr>
}
</form>
</table>

How to import data from checkbox (html) to database ASP.NET Core MVC

I have a list of guests which is stored in my database. After a party is created, I want the user to be able to invite guests to the party.
To do that, I have two tables with a one-to-many relationship. In the first table (guests), I have the guests' information, and in the second table (PartyGuests), there are two other columns for referencing other stuff.
After the user creates a party, a list of guests will appear in which the user can select whoever he wants to invite by selecting checkboxes. Clicking on the Submit button passes all selected guests id to the controller.
Here is my View.
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Operation</th>
</tr>
</thead>
<tbody>
#foreach (var item in ViewBag.Guests)
{
<tr>
<td>#item.FirstName</td>
<td>#item.LastName</td>
<td><input type="checkbox" name="guests" value="#item.Id"></td>
</tr>
}
</tbody>
</table>
<input type="hidden" value="#Model.Id" name="eventId"/>
<button type="submit" class="btn btn-primary">Invite</button>
</form>
Here is my controller:
[HttpGet]
public IActionResult Invite(int id)
{
ViewBag.Guests= _guestService.GetGuests(); //Display list of guests for invitation
ViewBag.Id = id;
return View(_eventService.GetEvent(id)); // Return event to get the event information o the Invite page
}
[HttpPost]
public IActionResult Invite(int eventId, string[] guests)
{
foreach (var item in guests)
{
var newGuest = new EventGuest
{
EventId = eventId,
GuestId = int.Parse(item)
};
// LINQ -- To avoid invite a guest more than once
if (_eventService.IsInvited(newGuest.GuestId, eventId)) // Does not invite
{
return RedirectToAction("Error", "Something"); // Give an error message
}
else
{
_eventService.InviteGuest(newGuest);
_eventService.SaveChanges();
}
}
return View("Index", "Home");
}
Now, the problem is that I want the invited guest checkboxes to be checked when they already invited. In another word, if the party manager wants to add more, he can be able to recognize the invited guests by looking at the checked checkboxes.
I should note that my database get valid data in its columns through this operation
I am not familiar with JavaScript, so please help me with HTML.
Thank you
The first thing to note is that you have a string array for guests in the action but I suspect this should be an array integers. You could also have a generic list of integers which should also work.
The following worked for me - note that if no checkboxes are checked then the list of ID's is empty
<form method="post" asp-area="YourArea" asp-controller="YourController" asp-action="Test">
<button type="submit">Post</button>
<input type="hidden" value="123" name="eventId" />
#for (int index = 0; index < Model.Count; index++)
{
<input type="checkbox" name="checkedIdList" value="#Model[index].Id" />
}
</form>
[HttpPost]
public async Task<IActionResult> Test(int eventId, List<int> checkedIdList)
{
await Task.CompletedTask;
return RedirectToAction("Index");
}

Angular JS, multiple insert error

I want to fetch data from SQL and display in HTML table using ng-repeat option then I need to edit some values in the table cell. My problem is that I only get initial values in the controller and the changes are not reflected in the controller. Here is my code:
app.controller('CRUD_EntryController', function ($scope, CRUD_InternalEntryService) {
GetStudentMarkDetails();
function GetStudentMarkDetails() {
var PromiseGetMarks = CRUD_InternalEntryService.GetMarkDetails();
PromiseGetMarks.then(function (res) {
$scope.MarkList = res.data;
})
}
$scope.mark = {};
$scope.save = function (MarkList) {
var index = 0;
$scope.MarkList.forEach(function (mark) {
console.log('rows #' + (index++) + ': ' + JSON.stringify(mark));
alert(mark.M1);
}
}
View:
<table class=" table table-condensed" id="myresul">
<tr>
<th>Slno</th>
<th>Name</th>
<th>RegNo</th>
<th>ClassNo</th>
<th>M1</th>
<th>M2</th>
<th>M3</th>
</tr>
<tbody data-ng-repeat="mark in MarkList" >
<tr>
<td class="col-md-1" >#{{$index+1}}</td>
<td class="col-md-2" ng-model="mark.Fname">{{mark.Fname}}</td>
<td class="col-md-2">{{mark.RegNo}}</td>
<td class="col-md-1">{{mark.ClassNo}}</td>
<td class="col-md-1"><input type="number" value="{{mark.M1}}" ng-model="M1" class="form-control" /></td>
<td class="col-md-1"><input type="number" value="{{mark.M2}}" ng-model="M2" class="form-control" /></td>
<td class="col-md-1"><input type="number" value="{{mark.M3}}" ng-model="M3" class="form-control" /></td>
</tr>
<button data-ng-click="save(MarkList)" class="btn btn-success">Save</button>
</tbody>
</table>
Don't think you need to define this: $scope.mark = {}; since mark is set in the scope of your ng-repeat. Remove it because this is somewhat confusing and might cause errors in the future.
Remove the value="{{mark.M1}}" and bind your model to ng-model="{{mark.M1}}". Asuming that you wand to bind to M1, M2 and M3 in your inputs.
Also see the angular docs for ngModel for more details and update your code accordingly.
By the way you don't have to pass MarkList as an argument for Save(..), you can do this:
<button data-ng-click="save()" class="btn btn-success">Save</button>, change the Save method signature to Save() and use $scope.MarkList instead of the argument MarkList.
Or change your method to only save the specific mark instead of the entire list every time.

.NET MVC 4 - Button actions in a foreach loop

I've currently got a View like this:
#using (Html.BeginForm("Compare", "Carousel", FormMethod.Post))
{
#foreach (var product in Model.Products)
{
<tr>
<td>#product.Provider.Name</td>
<td>£#Math.Round(product.MonthlyPremium, 2, MidpointRounding.AwayFromZero)</td>
<td>#Html.ActionLink("More Details", "MoreDetails", new { id = product.ProductId })</td>
<td><button name="compare" value="#product.ProductId">Compare</button</td>
</tr>
}
}
Controller:
[HttpPost]
public ActionResult Compare(ProductsWithDetailModel model) // This is a guess, Model may not persist?
{
// Code here
return View("Index", model);
}
What I actually want is, when a button is clicked, the Controller is going to be called and the product that has been selected (the button clicked) will be added to a list of items.
How do I actually find out which button has been clicked in the Controller? How come I cant find any tutorials out there on the web that have done this before, surely its basic functionality? Or am I approaching it wrong?
ASP.NET MVC takes all inputs and send it by a verb to an action. The bind of these controls is made by the name attribute on your View and the value is made by depending of what control we are talking about (text, checkbox, radio, select, button etc...). You could have a a property on your ProductsWithDetailModel called compare and it would recevie the value of the button you've clicked.
How about having 1 form per item?
E.g.
#foreach (var product in Model.Products)
{
<tr>
<td>#product.Provider.Name</td>
<td>£#Math.Round(product.MonthlyPremium, 2, MidpointRounding.AwayFromZero)</td>
<td>#Html.ActionLink("More Details", "MoreDetails", new { id = product.ProductId })</td>
<td>
#using (Html.BeginForm("Compare", "Carousel", FormMethod.Post))
{
<input type="hidden" name="productId" value="#product.ProductId" />
<button name="compare">Compare</button>
}
</td>
</tr>
}
use onclick. set js hidden value in loop. in controller check.

Categories