❔ how To create a view that can accept range of inputs, for models range for DbSet.AddRange method

This is my View Model where i am sending an Ienumarable of Model Option to get range of option for the question
22 Replies
mer_nam hai bulla rakhta hu kula
this is my controller method to pass ViewModel
Angius
Angius2y ago
What do you mean? Just
@model ModelsVM

<h1>@Model.Question.Whatever</h1>

<ul>
@foreach (var thing in Model.OptionsList)
{
<li>@thing.Whatever</li>
}
</ul>
@model ModelsVM

<h1>@Model.Question.Whatever</h1>

<ul>
@foreach (var thing in Model.OptionsList)
{
<li>@thing.Whatever</li>
}
</ul>
mer_nam hai bulla rakhta hu kula
@model OnlineExam.Models.ViewModels.OptionsVM @{ ViewData["Title"] = "Edit"; } <h1>Question Edit</h1> <form method="post"> <div class="container"> <div class="row"> <h4>Edit Question or Add Options</h4> </div> </div> <div> <label asp-for="question.question">Question</label> <input asp-for="question.question" /> <span asp-validation-for="question.question"></span> </div> @foreach(var obj in Model.OptionsList) { <div> <label asp-for="@obj.option"></label> <input asp-for="@obj.option" disabled/> </div> } @<div> <label asp-for="OptionsList"></label> <input asp-for="@obj.option" /> </div> <div> <label asp-for="@obj.option"></label> <input asp-for="@obj.option" disabled /> </div>@ <button type="submit" class="btn btn-primary">Update</button> <a asp-controller="Question" asp-action="Index">Go Back</a>
</form> this my view
Angius
Angius2y ago
Yeah And what doesn't work about it? Ah, wait, the issue is with how to create the form? So that it sends multiple fields to the controller?
Angius
Angius2y ago
Might need to name those fields manually That is,
@for(var i = 0; i < @Model.Questions.Length; i++)
{
<input type="text" name="Questions[@i]" id="Questions[@i]">
}
@for(var i = 0; i < @Model.Questions.Length; i++)
{
<input type="text" name="Questions[@i]" id="Questions[@i]">
}
and you should be able to get it with
public async Task OnGet(List<string> Questions)
{

}
public async Task OnGet(List<string> Questions)
{

}
mer_nam hai bulla rakhta hu kula
no no please come to coice chat *voice i will share my screen
Angius
Angius2y ago
I can't right now, sorry
mer_nam hai bulla rakhta hu kula
GitHub
GitHub - digvijay-ship-it/Online-Exam
Contribute to digvijay-ship-it/Online-Exam development by creating an account on GitHub.
mer_nam hai bulla rakhta hu kula
this my project repo what i am trying to do is adding 4 input fields in view
Angius
Angius2y ago
Then add those 4 input fields
mer_nam hai bulla rakhta hu kula
to take Input for Model Option's option(which Is a property)
Angius
Angius2y ago
Well, did you try the way I posted?
MODiX
MODiX2y ago
Angius#1586
That is,
@for(var i = 0; i < @Model.Questions.Length; i++)
{
<input type="text" name="Questions[@i]" id="Questions[@i]">
}
@for(var i = 0; i < @Model.Questions.Length; i++)
{
<input type="text" name="Questions[@i]" id="Questions[@i]">
}
and you should be able to get it with
public async Task OnGet(List<string> Questions)
{

}
public async Task OnGet(List<string> Questions)
{

}
React with ❌ to remove this embed.
Angius
Angius2y ago
With naming them manually? Ah, wait, I think I can see the issue... Each answer has its own ID and stuff, so just sending a list of strings won't do
mer_nam hai bulla rakhta hu kula
yes thats why i added Option as Ienumarable<Option> in ViewModel that option is connected to Question Model vie FK
Angius
Angius2y ago
Unless, perhaps,
@foreach(var q in Model.Questions)
{
<input
type="text"
name="Questions[@q.Id]"
id="Questions[@q.Id]"
value="@q.Body">
}
@foreach(var q in Model.Questions)
{
<input
type="text"
name="Questions[@q.Id]"
id="Questions[@q.Id]"
value="@q.Body">
}
public async Task OnPost(Dictionary<int, string> Questions)
{

}
public async Task OnPost(Dictionary<int, string> Questions)
{

}
Angius
Angius2y ago
Also, as a side note, line 59 of QuestionsController
if(id == null&&id==0)
if(id == null&&id==0)
id cannot be both null and 0 at the same time In any case, try the dictionary If that fails, I'm out of ideas, sorry
mer_nam hai bulla rakhta hu kula
sorry i shuld have put ||
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server