ASP.NET MVC

How do I check that my input (html) (from the view) matches the data in the database? (For example, like matching Username and Password). While also using DataAnnotations to validate that the input = database stuff? Also would like someone to do some pair coding to help me out if they have time.
23 Replies
BlasterBlaster
BlasterBlasterOP12mo ago
[HttpPost]
public IActionResult Save(List<Question> questions)
{
if(ModelState.IsValid)
{
foreach (Question question in questions)
{
question.Genus == *HtmlInput //i dont know the syntax of doing this, but basically some way to use the HTML input in my controller
}
}

}
[HttpPost]
public IActionResult Save(List<Question> questions)
{
if(ModelState.IsValid)
{
foreach (Question question in questions)
{
question.Genus == *HtmlInput //i dont know the syntax of doing this, but basically some way to use the HTML input in my controller
}
}

}
Angius
Angius12mo ago
If by "html input" you mean a form, then you can bind to the form data
[HttpPost]
public IActionResult Foo(string name)
{

}
[HttpPost]
public IActionResult Foo(string name)
{

}
<form method="post" action="some-controller">
<input type="text" name="name" />
<input type="submit" value="Send" />
</form>
<form method="post" action="some-controller">
<input type="text" name="name" />
<input type="submit" value="Send" />
</form>
BlasterBlaster
BlasterBlasterOP12mo ago
I was using a form. So, I can "bind" that parameter in the controller to my "<input>" using a tag-helper?
Angius
Angius12mo ago
No need for tag helpers Input named name goes into parameter named name
BlasterBlaster
BlasterBlasterOP12mo ago
[HttpPost]
public IActionResult Save(List<Question> questions, string input)
{
if(ModelState.IsValid)
{
foreach (Question question in questions)
{
if (question.Genus != input)
ModelState.AddModelError("", question.Species + " is wrong!");
}
}
return View(questions);

}
[HttpPost]
public IActionResult Save(List<Question> questions, string input)
{
if(ModelState.IsValid)
{
foreach (Question question in questions)
{
if (question.Genus != input)
ModelState.AddModelError("", question.Species + " is wrong!");
}
}
return View(questions);

}
Updated. Still don't know if it works.
Angius
Angius12mo ago
Well, try it? If that doesn't work, you might need [FromForm] attribute on the parameter Also, where are you getting the questions from?
BlasterBlaster
BlasterBlasterOP12mo ago
<form asp-action="Save" method="post">
<div asp-validation-summary="All" class="text-danger"></div>

@foreach(var question in Model)
{
<div class="form-group">
<label>@question.Species</label>
<input asp-controller="Game" asp-action="Save" asp-route="input" class="form-control" type="text" placeholder="@question.Species is a type of...">
</div>
}
</form>
<form asp-action="Save" method="post">
<div asp-validation-summary="All" class="text-danger"></div>

@foreach(var question in Model)
{
<div class="form-group">
<label>@question.Species</label>
<input asp-controller="Game" asp-action="Save" asp-route="input" class="form-control" type="text" placeholder="@question.Species is a type of...">
</div>
}
</form>
"questions" is coming from the razor model (@model List<Question>) on the top of my razor page.
BlasterBlaster
BlasterBlasterOP12mo ago
No description
BlasterBlaster
BlasterBlasterOP12mo ago
Above. Is the example of my code running.
Angius
Angius12mo ago
Ah, so you want a list of inputs, essentially, gotcha
BlasterBlaster
BlasterBlasterOP12mo ago
@ZZZZZZZZZZZZZZZZZZZZZZZZZ What do you think about my using of the asp tag helpers like asp-controller , asp-action, and asp-route in my input?
Angius
Angius12mo ago
You'll have to look up how to deal with dynamic forms, then
BlasterBlaster
BlasterBlasterOP12mo ago
It's a game where you have to match the definitions basically
Angius
Angius12mo ago
Inputs don't have any URL-like attributes, so asp-controller, asp-action, etc. are useless on them The form defines where the data is sent So the form needs that
BlasterBlaster
BlasterBlasterOP12mo ago
@model List<Question>


<div class="text-center">
<h1 class="display-4">Welcome</h1>

</div>
<form asp-controller="Game" asp-action="Save" method="post">
<div asp-validation-summary="All" class="text-danger"></div>

@foreach(var question in Model)
{
<div class="form-group">
<label>@question.Species</label>
<input name="input "class="form-control" type="text" placeholder="@question.Species is a type of...">
</div>
}

</form>
@model List<Question>


<div class="text-center">
<h1 class="display-4">Welcome</h1>

</div>
<form asp-controller="Game" asp-action="Save" method="post">
<div asp-validation-summary="All" class="text-danger"></div>

@foreach(var question in Model)
{
<div class="form-group">
<label>@question.Species</label>
<input name="input "class="form-control" type="text" placeholder="@question.Species is a type of...">
</div>
}

</form>
This is what I changed it to So the "name" tag helps connect the view to the controller? Esp. the parameter
Angius
Angius12mo ago
Yes Input with [name] -> parameter with [name] But in your case, you have a dynamic list of inputs Each should have an individual name, too, since they refer to different questions
BlasterBlaster
BlasterBlasterOP12mo ago
Ah ssibal
Angius
Angius12mo ago
So, again, you should look up how to deal with dynamic forms
BlasterBlaster
BlasterBlasterOP12mo ago
Ok I will One more thing, can I use the "Enter" button to submit my <input> or do I need to connect a button to it?
Angius
Angius12mo ago
Might need Javascript for that
BlasterBlaster
BlasterBlasterOP12mo ago
Okay, I don't know Javascript haha so maybe just use a button Going to bump my thread to see if anyone can help me. I am still having issues with my program.
using FinalProject.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace FinalProject.Controllers
{
public class GameController : Controller
{
private FinalProjectContext _context;
public GameController(FinalProjectContext context)
{
_context = context;
}

public IActionResult Index()
{
List<Question> questions = _context.Questions.ToList();
GameViewModel gameViewModel = new GameViewModel();
gameViewModel.Questions = questions;
return View(gameViewModel);
}


[HttpPost]
public IActionResult Save(GameViewModel gameViewModel)
{

if (ModelState.IsValid)
{
foreach (Question question in gameViewModel.Questions)
{
//if (question.Genus != gameViewModel.UserInput)
// ModelState.AddModelError("", question.Species + " is wrong!");
string test = "Genus is " + question.Genus + " input is " + gameViewModel.UserInput;

}
return RedirectToAction("Index", "Home");
}
else
{
return View(gameViewModel);
}


}



}
}
using FinalProject.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace FinalProject.Controllers
{
public class GameController : Controller
{
private FinalProjectContext _context;
public GameController(FinalProjectContext context)
{
_context = context;
}

public IActionResult Index()
{
List<Question> questions = _context.Questions.ToList();
GameViewModel gameViewModel = new GameViewModel();
gameViewModel.Questions = questions;
return View(gameViewModel);
}


[HttpPost]
public IActionResult Save(GameViewModel gameViewModel)
{

if (ModelState.IsValid)
{
foreach (Question question in gameViewModel.Questions)
{
//if (question.Genus != gameViewModel.UserInput)
// ModelState.AddModelError("", question.Species + " is wrong!");
string test = "Genus is " + question.Genus + " input is " + gameViewModel.UserInput;

}
return RedirectToAction("Index", "Home");
}
else
{
return View(gameViewModel);
}


}



}
}
This is my controller
@model GameViewModel


<div class="text-center">
<h1 class="display-4">Welcome</h1>

</div>
<form asp-controller="Game" asp-action="Save" method="post">
<div asp-validation-summary="All" class="text-danger"></div>

@foreach(var question in Model.Questions)
{
<div class="form-group">
<label>@question.Species</label>
<input name="GameViewModel.UserInput" class="form-control" type="text" placeholder="@question.Species is a type of...">
<input type="hidden" name="GameViewModel.Questions.Genus" value="@question.Genus">
</div>
}
<button type="submit">Submit</button>
</form>
@model GameViewModel


<div class="text-center">
<h1 class="display-4">Welcome</h1>

</div>
<form asp-controller="Game" asp-action="Save" method="post">
<div asp-validation-summary="All" class="text-danger"></div>

@foreach(var question in Model.Questions)
{
<div class="form-group">
<label>@question.Species</label>
<input name="GameViewModel.UserInput" class="form-control" type="text" placeholder="@question.Species is a type of...">
<input type="hidden" name="GameViewModel.Questions.Genus" value="@question.Genus">
</div>
}
<button type="submit">Submit</button>
</form>
This is my view
BlasterBlaster
BlasterBlasterOP12mo ago
No description
BlasterBlaster
BlasterBlasterOP12mo ago
This is my website I don't understand how the form is submitting I am not able to transfer my data to the controller If I switch my asp-action="Save" in my view then it will crash on submitting Saying it can't find a save view One thing is that my ModelState.IsValid is returning false, and I don't know why this is. I want it to send my viewmodel to my post Ok well I've made progress But my current issue is now my data is not being passed correctly to my controller I think it is how i coded my view But this is very difficult for me I think my ViewModel is formatted wrong for what I want for my game to work aswell
Want results from more Discord servers?
Add your server