I'm working in entityframework 6.
I have a problem with my Checkbox. I'm trying to select multiple of choices then save these choices to a List, save them to my PostModel which holds an ICollection<TagLogs>.
Like any other i scoured the net for a solution, and followed some, but to no answer.
I am getting this error:
InvalidOperationException: Unexpected 'asp-for' expression result type 'System.Collections.Generic.List`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' for . 'asp-for' must be of type 'System.Boolean' or 'System.String' that can be parsed as a 'System.Boolean' if 'type' is 'checkbox'. I understand i need to change the SelectedChanges and make it to a Boolean or a string, then change few other things, but i still got more errors.
[BindProperty]
public List<int> SelectedChanges { get; set; } = new List<int>();
public IEnumerable<TagLog> TagLogs { get; set; }
public async Task OnGet()
{
//ChangeLogs = changeLogRepository.GetAllModels();
TagLogs = await context.TagLogs.ToListAsync();
//SelectedChanges = new List<int>();
}
public async Task<IActionResult> OnPostAsync()
{
if (ModelState.IsValid)
{
var newPost = context.PostModels.FirstOrDefault(p => p.Title == PostViewModel.Title);
if (newPost == null)
{
if (TagLogs != null)
{
var selectedType = TagLogs.Where(x => SelectedChanges.Contains(x.TagLogId)).ToList();
PostModel postModel = new PostModel
{
Title = PostViewModel.Title,
ChangeLogs = selectedType,
};
}
postRepository.Add(postModel);
TagLogs = changeLogRepository.GetAllModels();
}
return RedirectToPage("/Index");
}
return RedirectToPage("/Index");
}
FailedToPost = "Failed to Post, try again.";
return Page();
}
public class TagLog
{
[Key]
public int TagLogId { get; set; }
public string TagLogName { get; set; }
public bool IsChecked { get; set; }
public virtual ICollection<PostModel>? PostModels { get; set; }
}
Front End
#foreach (var change in Model.TagLogs)
{
<div class="form-check">
<!-- Set the value of the input to the change ID -->
<input class="form-check-input" type="checkbox" asp-for="SelectedChanges" value="#change.TagLogId" />
<label class="form-check-label">#change.TagLogName</label>
</div>
}
</div>
Best regards.
With the Help of a little ChatGPT and some skills of my own i managed to fix it. Ofcourse tons of stackoverflows. I hope my answer will help others.
Also want to thank #Sebastian Siemens for pointing out things.
What i was missing was loading the Taglogs before i compared where SelectedTaglos and SelectedChanged contained same id. Now this might be some bad and rookie mistake, but it took more time than i would like to admit.
<div class="form-group">
<h4>Select Multiple Logs</h4>
#foreach (var change in Model._tagLogs)
{
<div class="form-check">
<input class="form-check-input" type="checkbox" name="SelectedChanges" value="#change.TagId"
#(Model.SelectedChanges.Contains(change.TagId) ? "checked" : "") />
<label class="form-check-label">#change.TagTyp</label>
</div>
}
</div>
public async Task<IActionResult> OnPostAsync()
{
if (ModelState.IsValid)
{
var newPost = context.PostModels.FirstOrDefault(p => p.Title == PostViewModel.Title);
if (newPost == null)
{
_tagLogs = await context._TagLogs.ToListAsync();
// takes multiple checkbox values
var selectedTagLogs = _tagLogs.Where(x => SelectedChanges.Contains(x.TagId)).ToList();
if (selectedTagLogs != null)
{
PostModel postModel = new PostModel
{
Title = PostViewModel.Title,
_TagLogs = selectedTagLogs,
};
postRepository.Add(postModel);
_tagLogs = changeLogRepository.GetAllModels();
}
_tagLogs = changeLogRepository.GetAllModels();
}
}
}
return Page();
}
Related
My problem is this: I have created a view in which I want to be able to remove and add new identity roles. For that I created two forms that call two different actions. I have created a big model which consists of smaller models which are bound by the forms to the actions. Everything works as intended, models get bound properly, data is there and proper actions are executed as they should.
Don't get me wrong, I understand those are separate forms and I know they will never show validation messages both at the same time because only one form can be posted at a time. The problem is that when I click submit on the CreateNewRole form, the validation message is there, but when I click submit on the RemoveRoles action, I don't get any validation messages and no validation classes are applied to the html elements, there is the usual "field-validation-valid" and no "field-validation-error" class in it while its perfectly present in CreateNewRole.
Why does validation work on the first form but not on the second form? I cant find any difference in how i made those two forms, am i not noticing something?
When I debug modelstate of the RemoveRoles action, the error is there, but somehow the view isn't getting informed of it.
View
#model Models.ViewModels.RolePanelViewModel
#using Extensions
<h2>Role Panel</h2>
<article>
<div class="admin-panel-flex-container">
<div class="form-container">
#using (Html.BeginForm("CreateNewRole", "Administrator"))
{
<div class="form-group">
#Html.LabelFor(l => l.NewRoleRolePanelViewModel.NewIdentityRole.Name)
#Html.TextBoxFor(t => t.NewRoleRolePanelViewModel.NewIdentityRole.Name)
#Html.ValidationMessageFor(t => t.NewRoleRolePanelViewModel.NewIdentityRole.Name)
<button type="submit">Dodaj role</button>
</div>
}
</div>
<div class="form-container">
#using (Html.BeginForm("RemoveRoles", "Administrator"))
{
<div class="form-group">
#Html.LabelFor(l => l.ListRolePanelViewModel.SelectedIdentityRoles)
#Html.ListBoxFor(l => l.ListRolePanelViewModel.SelectedIdentityRoles, #Model.ListRolePanelViewModel.IdentityRolesSelectListItems)
#Html.ValidationMessageFor(t => t.ListRolePanelViewModel.SelectedIdentityRoles)
<button type="submit">Skasuj wybrane</button>
</div>
}
</div>
</div>
</article>
Models
public class RolePanelViewModel
{
public ListRolePanelViewModel ListRolePanelViewModel { get; set; }
public NewRoleRolePanelViewModel NewRoleRolePanelViewModel { get; set; }
}
public class ListRolePanelViewModel
{
public List<IdentityRoleDTO> IdentityRoles { get; set; }
public List<SelectListItem> IdentityRolesSelectListItems { get; set; }
[Required(ErrorMessage = "Należy wybrać przynajmniej jedną pozycję z listy")]
public List<string> SelectedIdentityRoles { get; set; }
}
public class NewRoleRolePanelViewModel
{
public IdentityRoleDTO NewIdentityRole { get; set; }
}
public class IdentityRoleDTO
{
public string Id { get; set; }
[Required(ErrorMessage = "Nowa rola musi mieć nazwę")]
[MinLength(5)]
public string Name { get; set; }
public List<IdentityUserRole> Users { get; set; }
}
Actions
public ActionResult OpenRolePanel()
{
var roles = _context.Roles.ToList();
var viewModel = new RolePanelViewModel
{
ListRolePanelViewModel = new ListRolePanelViewModel
{
IdentityRolesSelectListItems = GetSelectListItems(roles,
(a) => new SelectListItem {Value = a.Id.ToString(), Selected = false, Text = a.Name})
},
NewRoleRolePanelViewModel = new NewRoleRolePanelViewModel()
};
return View("RolePanel", viewModel);
}
[HttpPost]
public async Task<ActionResult> CreateNewRole(NewRoleRolePanelViewModel newRoleRolePanelViewModel)
{
if(!ModelState.IsValid)
{
var roles = _context.Roles.ToList();
var viewModel = new RolePanelViewModel
{
ListRolePanelViewModel = new ListRolePanelViewModel
{
IdentityRolesSelectListItems = GetSelectListItems(roles,
(a) => new SelectListItem { Value = a.Id.ToString(), Selected = false, Text = a.Name })
},
NewRoleRolePanelViewModel = newRoleRolePanelViewModel
};
return View("RolePanel", viewModel);
}
var roleStore = new RoleStore<IdentityRole>(new ApplicationDbContext());
var roleManager = new RoleManager<IdentityRole>(roleStore);
await roleManager.CreateAsync(new IdentityRole(newRoleRolePanelViewModel.NewIdentityRole.Name));
return View("RolePanel");
}
[HttpPost]
public ActionResult RemoveRoles(ListRolePanelViewModel listRolePanelViewModel)
{
if (!ModelState.IsValid)
{
var roles = _context.Roles.ToList();
var viewModel = new RolePanelViewModel
{
ListRolePanelViewModel = listRolePanelViewModel,
NewRoleRolePanelViewModel = new NewRoleRolePanelViewModel()
};
viewModel.ListRolePanelViewModel.IdentityRolesSelectListItems = GetSelectListItems(roles,
(a) => new SelectListItem {Value = a.Id.ToString(), Selected = false, Text = a.Name});
return View("RolePanel", viewModel);
}
return View("RolePanel");
}
Custom method that may be needed if you want to run the code
private List<SelectListItem> GetSelectListItems<T>(List<T> dbSetResult, Func<T,SelectListItem> Func)
{
var result = new List<SelectListItem>();
foreach (var item in dbSetResult)
{
result.Add(Func(item));
}
return result;
}
I have a simple select with an option selected from the model that is loaded.
To start here is my simple model
public class invoice
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public invoice_state invoice_state { get; set; }
}
public class invoice_state
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string alias { get; set; }
public List<invoice> invoices { get; set; }
[Display(Name = "title")]
public string title { get; set; }
}
Here is what I have that works in one view.
in controller:
public IActionResult Create()
{
string state = "start_finish";
ViewBag.State = state;
var states = _context.invoice_state.Select(c => new {
id = c.ID,
title = c.title
}).ToList();
ViewBag.States = new SelectList(states, "id", "title", _context.invoice_state.Where(e => e.alias == state).FirstOrDefault().ID);
return View();
}
in the view
#Html.DropDownList("invoice_state", (SelectList)ViewBag.States, "--Select One--")
That works fine, the option is selected... but on my edit view which is set up mostly the same is not working.
in controller:
public async Task<IActionResult> Edit(int? id)
{
var invoice = await _context.invoice
.Include(_item => _item.invoice_state).SingleOrDefaultAsync(m => m.ID == id);
if (invoice == null)
{
return NotFound();
}
string state = "start_finish"; // as a default
var states = _context.invoice_state.Select(c => new { id = c.ID, title = c.title }).ToList();
if (null != invoice.invoice_state)
{
ViewBag.State = invoice.invoice_state.alias;
}
else
{
ViewBag.State = state;
}
ViewBag.States = new SelectList(states, "id", "title", _context.invoice_state.Where(e => e.alias == state).FirstOrDefault());
return View(invoice);
}
and in the edit view
#Html.DropDownList("invoice_state", (SelectList)ViewBag.States, "--Select One--")
I have read all over the place and can't find a simple answer that isn't wire up more files, and even those haven't helped get me to the need. I have tried to force it too and it is not working, but figured it was worth placing here too.
ViewBag.States = _context
.invoice_state
.Select(c => new SelectListItem {
Value = c.ID.ToString(),
Text = c.title
})
.Select(l => new SelectListItem {
Selected = (l.Value == invoice.invoice_state.ID.ToString()),
Text = l.Text,
Value = l.Value
});
but only get 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable<Microsoft.AspNetCore.Mvc.Rendering.SelectListItem>' to 'Microsoft.AspNetCore.Mvc.Rendering.SelectList' or the list version of the error if i add .ToList() on the select like before.
Some people have suggested to set the selected value, and as i read it it would be like,
if (null != invoice.invoice_state)
{
ViewBag.invoice_stateID = invoice.invoice_state.ID;
}
else
{
ViewBag.invoice_stateID = _context.invoice_state.Where(e => e.alias == "start_finish").FirstOrDefault().ID;
}
if i use
<select asp-for="invoice_state" asp-items="#ViewBag.States" >
<option>Please select one</option>
</select>
It doesn't work either, see the list but nothing selected. Last note, if I select it and submit it, it does set the value in the database, just when I get back to the edit it again fails to again select anything.
Also to be clear there is the data
which came from
<dt>
#Html.DisplayNameFor(model => model.invoice_state)
</dt>
<dd>
#Html.DisplayFor(model => model.invoice_state)
</dd>
#Stephen Muecke is right, the ticket is the ViewModel. Not a fan fully here but I'll simmer. The solution is not just getting it to show, you need to save is too. Here is the whole of it.
ViewModel
namespace project_name.Models.InvoiceViewModel
{
public class EditInvoiceViewModel
{
public int invoice_stateID { get; set; }
public invoice invoice { get; set; }
public List<SelectListItem> States { set; get; }
}
}
edit in action on controller
public async Task<IActionResult> Edit(int? id)
{
var invoice = await _context.invoice
.Include(_item => _item.invoice_state).SingleOrDefaultAsync(m => m.ID == id);
string state = "start_finish"; // as a default
var states = _context.invoice_state.Select(c => new { id = c.ID, title = c.title }).ToList();
if (null != invoice.invoice_state)
{
ViewBag.State = invoice.invoice_state.alias;
}
else
{
ViewBag.State = state;
}
var vm = new EditInvoiceViewModel();
vm.invoice = invoice;
vm.States = _context
.invoice_state
.Select(c => new SelectListItem
{
Value = c.ID.ToString(),
Text = c.title
})
.ToList();
if (null != invoice.invoice_state)
{
vm.invoice_stateID = invoice.invoice_state.ID;
} else {
vm.invoice_stateID = _context.invoice_state.Where(e => e.alias == "start_finish").FirstOrDefault().ID;
}
return View(vm);
}
to save in action on controller
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(EditInvoiceViewModel model)
{
if (ModelState.IsValid)
{
try
{
model.invoice.creator = await GetCurrentUserAsync();
model.invoice.invoice_state = await _context.invoice_state.SingleOrDefaultAsync(m => m.ID == model.invoice_stateID);
_context.Update(model.invoice);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!invoiceExists(model.invoice.ID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction("Index");
}
return View(model);
}
And in the view
<div class="form-group">
<label asp-for="#Model.invoice_stateID" class="col-md-2 control-label"></label>
<select asp-for="#Model.invoice_stateID" asp-items="#Model.States" >
<option>Please select one</option>
</select>
</div>
The thing I guess I was having a hard time with is all supporting questions that are like this, and all the blogs focus on the display, but i still need to save it, and that was not clear. I had to skip the [bind()] but I'll get back to that.
I have a problem with an application using ASP.NET MVC 4, I'm trying to show in a view to create an entity called CompositePiece, a List of items (which are CompositePieces too) from my database, then, the user could select one or more of them, and the items selected would be in a collection in the 'father' CompositePiece, at first, my class CompositePiece:
namespace Garbi.Models{
public class CompositePiece
{
[Key]
public int CompositePieceId { get; set; }
[Required(ErrorMessage = "Introduzca un nombre para la pieza")]
[Display(Name = "Nombre de la pieza")]
public string CompositePieceName { get; set; }
public virtual ICollection<CompositePiece> Components { get; set; }
public int ProcessId { get; set; }
public int LevelOfHierarchy { get; set; }
public CompositePiece(PieceModel model)
{
this.Components = new List<CompositePiece>();
this.CompositePieceName = model.PieceModelName;
LevelOfHierarchy = 0;
}
public CompositePiece()
{
this.Components = new List<CompositePiece>();
LevelOfHierarchy = 0;
}
public CreateOrEditCompositePieceViewModel ToCreateOrEditCompositePieceViewModel(string processName)
{
return new CreateOrEditCompositePieceViewModel
{
CompositePieceName = this.CompositePieceName,
ProcessId = this.ProcessId,
ProcessName = processName
};
}
}
}
I have created a ViewModel to pass the data to a view, the ViewModel is:
namespace Garbi.ViewModels.CompositePieces
{
public class CreateOrEditCompositePieceViewModel
{
[Required(ErrorMessage = "Introduzca un nombre para la pieza")]
[Display(Name = "Nombre de la pieza")]
public string CompositePieceName { get; set; }
public virtual IEnumerable<SelectListItem> Components { get; set; }
public string[] SelectedComponentsId { get; set; }
public int ProcessId { get; set; }
public string ProcessName { get; set; }
public int LevelOfHierarchy { get; set; }
public void AddComponentsList(IEnumerable<CompositePiece> dataProvider, CompositePiece fatherPiece)
{
List<SelectListItem> auxList = new List<SelectListItem>();
foreach (CompositePiece piece in dataProvider)
{
SelectListItem item = new SelectListItem
{
Text=piece.CompositePieceName,
Value = piece.CompositePieceId.ToString(),
Selected = fatherPiece.Components.Contains(piece)
};
auxList.Add(item);
}
Components = new List<SelectListItem>(auxList);
}
public CompositePiece ToCompositePiece()
{
return new CompositePiece
{
CompositePieceName = this.CompositePieceName,
LevelOfHierarchy = this.LevelOfHierarchy,
ProcessId = this.ProcessId
};
}
}
}
Mi View is:
#model Garbi.ViewModels.CompositePieces.CreateOrEditCompositePieceViewModel
#{
ViewBag.Title = "Crear pieza compuesta";
}
<div class="advertise">Crear pieza compuesta para #Model.ProcessName</div>
#using (Html.BeginForm()) {
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>Nueva pieza compuesta</legend>
<div class="editor-label">
#Html.LabelFor(model => model.CompositePieceName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.CompositePieceName)
#Html.ValidationMessageFor(model => model.CompositePieceName)
</div>
<div class="editor-field">
#foreach (var item in Model.Components)
{
<div class="checkbox">
#Html.CheckBoxFor(i => item.Selected, item.Value)
#Html.HiddenFor(i => item.Value)
#item.Text
#Html.HiddenFor(i => item)
</div>
}
</div>
#Html.HiddenFor(model => model.Components)
#Html.HiddenFor(model => model.LevelOfHierarchy)
#Html.HiddenFor(model => model.ProcessId)
#Html.HiddenFor(model => model.ProcessName)
<p>
<input type="submit" value="Crear" />
</p>
</fieldset>
<div class="back">
#Html.NavLink("Process_"+#Model.ProcessId, "Page1", "Volver")
</div>
}
And at last, I want to show you the methods of my controller, they are:
public ActionResult ComposePieces(int processId)
{
context = new EFDbContext();
Process process = context.ProcessesPaginable.FindEntityById(processId);
CreateOrEditCompositePieceViewModel model = new CreateOrEditCompositePieceViewModel
{
ProcessId = process.ProcessId,
ProcessName = process.ProcessName,
LevelOfHierarchy = 0
};
IEnumerable<CompositePiece> totalPieces = context.CompositePiecesPaginable.FindAllCompositePiecesOfAProcess(processId).Where(p => p.LevelOfHierarchy == 0);
model.AddComponentsList(totalPieces, model.ToCompositePiece());
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ComposePieces(CreateOrEditCompositePieceViewModel model)
{
context = new EFDbContext();
CompositePiece compositePieceToAdd = model.ToCompositePiece();
CompositePiece aux;
[...]
}
My problem comes when I call the view, I can see perfectly the collection of items, each of them with its checkbox, but when I check some of them, put the CompositePiece's name and click at Submit button, I recover the new CreateOrEditCompositePieceViewModel with its new name, but my attribute Components is empty. I think I am having a mistake of base, but I don't know where is the error, and why I have this problem.
Thank you for your time and sorry for my bad english.
I also have the same Problem as you do.
When I get the postback, the Navigation properties are not returning, even thought I select them on the form, or hid them.
I think this Problem has to do with the Attribute being virtual, and by so it uses lazy loading.
One Thing that I tried that showed promising, but still did^'t get the Job done is if you add the:
#Html.HiddenFor(model => Model.Components);
Before your foreach Loop.
What happens is that when you look on you postback, you will find the components there, it won't be null anymore, but still the list will be empty, but I think the solution is on this direction.
Anyways, enough of this.
I'm about to do a way around this that should work.
What you do, you create a viewmodel
with your class
plus list of each of the virtual lists
something like this:
public class MyViewModel
{
public MyModelView(CompositePiece cp)
{
CompositePiece = cp;
Components = cp.Components.ToList();
}
public CompositePiece CompositePiece { get; set; }
public List<Component> Components { get; set; }
}
I just saw that on your class you create a list of itself, you probably shouldn't do that.
That's not how you create linked lists in C#
So, if instead you do like this example, you will create an instance of you virtual lists, and this should solve the Problem.
Good luck.
I am trying to do this with one View, perhaps this is not the best way?
I have a form that is data driven; the questions all come from a Questions table but the answers populate a Response table. here is the View;
#using Microsoft.AspNet.Identity
#model Template.Models.Question
#{
ViewBag.Title = "View question";
var qtype = Model.QuestionTypeId;
ViewBag.Number = Model.Id - 7;
Html.BeginForm("ViewQuestion", "Question", FormMethod.Post, new { #class = "form-horizontal", role = "form" });
}
<h4>Question ##ViewBag.Number</h4>
<hr />
<h1> #Model.Question1</h1>
#Html.AntiForgeryToken()
<div>
#switch (qtype)
{
case 1:
// Textbox
#Html.TextArea("Answer", new { #class = "form-control", rows = "4", col = "5" });<br /><br />
break;
case 2:
// Dropdown
<select class="form-control" id="Answer">
#foreach (var item in Model.QuestionOptions.OrderBy(o => o.QuestionOptionRanking))
{
<option value="#item.QuestionOption1">#item.QuestionOption1</option>
}
</select><br /><br />
break;
case 3:
// Checkbox
<div class="checkbox">
#foreach (var item in Model.QuestionOptions.OrderBy(o => o.QuestionOptionRanking))
{
<input type="checkbox" name="Answer" value="#item.QuestionOption1" /> #item.QuestionOption1 <br />
}
</div><br /><br />
break;
case 4:
// Radio buttons
foreach (var item in Model.QuestionOptions.OrderBy(o => o.QuestionOptionRanking))
{
<div class="radio">
<label>
<input type="radio" name="Answer" value="#item.QuestionOption1" />
#item.QuestionOption1
</label>
</div>
}<br /><br />
break;
}
</div>
<input type="hidden" name="QuestionId" value="#Model.Id" />
<input type="hidden" name="UserId" value="#User.Identity.GetUserId()" />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Answer" />
</div>
</div>
And here is my ViewModel;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Template.Models
{
public class GetQuestionViewModel
{
public int Id { get; set; }
public int PageNumber { get; set; }
public string Question1 { get; set; }
public int QuestionTypeId { get; set; }
public Nullable<int> LinkedTo { get; set; }
public Nullable<int> Options { get; set; }
public Nullable<int> QuestionRanking { get; set; }
public virtual ICollection<QuestionOption> QuestionOptions { get; set; }
public virtual QuestionType QuestionType { get; set; }
public virtual ICollection<Response> Responses { get; set; }
public virtual ICollection<AspNetUser> AspNetUsers { get; set; }
}
public class ResponseViewModel
{
[Required]
public string UserId { get; set; }
[Required]
public int QuestionId { get; set; }
[Required(ErrorMessage = "Please answer the question before submitting")]
public string Answer { get; set; }
[Required]
public string Source { get; set; }
[Required]
public string Status { get; set; }
[Required]
public System.DateTime DateStamp { get; set; }
public Nullable<int> Duplicate { get; set; }
public virtual Question Questions { get; set; }
public object SelectedValue { get; set; }
public virtual ICollection<QuestionOption> QuestionOptions { get; set; }
}
}
I cannot get the GetQuestionViewModel to work at all and have resorted to the .emdx version of the Question Model to populate the page, but my task is a) display each user's answers on the page, and b) display the questions on the PageNumber field as any number of questions can be displayed on a single screen.
Here is my controller but as you can see more is commented out than work!
using System.Web.Mvc;
using Template.Models;
namespace Template.Controllers
{
public class QuestionController : Controller
{
private WebTemplateEntities db = new WebTemplateEntities();
// GET: /Questions/ViewQuestion/5
public ActionResult ViewQuestion(int? id)
{
if (id == null || id == 0 || id > 12)
{
id = 8;
}
var question = db.Questions.Find(id);
if (question == null)
{
return HttpNotFound();
}
return View(question);
}
// [Route("Questions/{page?}")]
//public ActionResult ViewQuestion(GetQuestionViewModel model, int? id)
//{
// if (id == null || id == 0 || id > 12)
// {
// //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
// id = 8;
// //return RedirectToAction("/ViewQuestion/" + id);
// }
// var q = db.Questions.Find(id);
// var pageId = q.PageNumber;
// var questions = from q in db.Questions
// where q.PageNumber == pageId
// orderby q.QuestionRanking
// select q;
// return View(questions);
// }
// POST: /Questions/ViewQuestion/5
[HttpPost]
public ActionResult ViewQuestion([Bind(Include = "QuestionId, Answer, UserId")] ResponseViewModel responseViewModel)
{
Response re = new Models.Response();
re.Answer = responseViewModel.Answer;
if (re.Answer == null)
{
re.Answer = "Work in progress!";
// re.Answer = responseViewModel.SelectedValue();
// re.Answer = int.Parse(SelectList["Question.QuestionOption1"]);
}
re.UserId = responseViewModel.UserId;
re.QuestionId = responseViewModel.QuestionId;
var id = responseViewModel.QuestionId;
re.Source = "Web";
re.Status = "New";
re.DateStamp = System.DateTime.Now;
db.Responses.Add(re);
db.SaveChanges();
return RedirectToAction("ViewQuestion/" + (id + 1));
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
I have been working on this problem for literally days and now am just going around in circles. Any help or advice would me greatly appreciated.
It looks like you are confusing Entity models and ViewModels. In your view you reference Template.Models.Question, which appears to be an Entity rather than a ViewModel.
The view should only reference your ViewModel, and not your Entity. And there should be some mapping mechanism established between your Entity and your ViewModel. Another thing I noticed is that your Entity and your ViewModel appear to reside in the same namespace in your application. This isn't necessarily a problem, but can get confusing as ViewModels pertain to your view only and Entities pertain to your persistence layer.
Currently your code is getting a question object via this: from q in db.Questions which you are then passing into View(question). So you will not actually have your ViewModel available in the view, it will be your entity model.
Change your view to:
#model Template.Models.GetQuestionViewModel
And change your controller to something like this:
var questions = (from q in db.Questions
where q.PageNumber == pageId
orderby q.QuestionRanking
select q.ToViewModelExtensionMethod()).ToList();
return View(questions);
You'll have to write that extension method to perform the mapping from Template.Model.Question to Template.Model.GetQuestionViewModel.
Hope this helps.
Update
For info about extension methods, here's the MSDN article.
I just use extension methods because I like the way the syntax flows. Here's a quick example:
namespace Template.Models.Extensions
{
public static class QuestionExtensions
{
public static GetQuestionViewModel ToViewModelExtensionMethod(this Question question)
{
var result = new GetQuestionViewModel();
result.field1 = question.field1;
// ... etc
return result;
}
}
}
Also, note the change above where I am now mapping the items as they are queried.
I have the following form:
#model Teesa.Models.SearchModel
#using (Html.BeginForm("Index", "Search", FormMethod.Get, new { id = "SearchForm" }))
{
<div class="top-menu-search-buttons-div">
#if (!MvcHtmlString.IsNullOrEmpty(Html.ValidationMessageFor(m => m.SearchText)))
{
<style type="text/css">
.top-menu-search-text
{
border: solid 1px red;
}
</style>
}
#Html.TextBoxFor(q => q.SearchText, new { #class = "top-menu-search-text", id = "SearchText", name = "SearchText" })
#Html.HiddenFor(q=>q.Page)
<input type="submit" value="search" class="top-menu-search-submit-button" />
</div>
<div id="top-menu-search-info" class="top-menu-search-info-div">
Please Select one :
<hr style="background-color: #ccc; height: 1px;" />
<div class="top-menu-search-info-checkbox-div">
#Html.CheckBoxFor(q => q.SearchInBooks, new { id = "SearchInBooks", name = "SearchInBooks" })
<label for="SearchInBooks">Books</label>
</div>
<div class="top-menu-search-info-checkbox-div">
#Html.CheckBoxFor(q => q.SearchInAuthors, new { id = "SearchInAuthors" })
<label for="SearchInAuthors">Authors</label>
</div>
<div class="top-menu-search-info-checkbox-div">
#Html.CheckBoxFor(q => q.SearchInTags, new { id = "SearchInTags" })
<label for="SearchInTags">Tags</label>
</div>
}
and the following Controller and Models :
namespace Teesa.Models
{
public class SearchModel
{
[Required(ErrorMessage = "*")]
public string SearchText { get; set; }
public bool SearchInTags { get; set; }
public bool SearchInAuthors { get; set; }
public bool SearchInBooks { get; set; }
public int Page { get; set; }
public List<SearchBookModel> Result { get; set; }
public List<SimilarBookModel> LatestBooks { get; set; }
}
public class SearchBookModel
{
public int Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string Summary { get; set; }
public List<Tags> Tags { get; set; }
public string StatusName { get; set; }
public string SubjectName { get; set; }
public string ThumbnailImagePath { get; set; }
public string BookRate { get; set; }
public string RegistrationDate { get; set; }
public int NumberOfVisit { get; set; }
}
}
[HttpGet]
public ActionResult Index(SearchModel model)
{
FillSearchModel(model);
if (ModelState.IsValid)
{
string page = model.Page;
DatabaseInteract databaseInteract = new DatabaseInteract();
model.Result = new List<SearchBookModel>();
List<Book> allBooks = databaseInteract.GetAllBooks();
List<Book> result = new List<Book>();
#region
if (model.SearchInTags)
{
var temp = (from item in allBooks
from tagItem in item.Tags
where tagItem.Name.Contains(model.SearchText)
select item).ToList();
result.AddRange(temp);
}
if (model.SearchInBooks)
{
var temp = (from item in allBooks
where item.عنوان.Contains(model.SearchText)
select item).ToList();
result.AddRange(temp);
}
if (model.SearchInAuthors)
{
var temp = (from item in allBooks
where item.Author.Contains(model.SearchText)
select item).ToList();
result.AddRange(temp);
}
#endregion
#region Paging
string itemsPerPage = databaseInteract.GetItemsPerPage();
int ItemInPage = int.Parse(itemsPerPage);
var pagingParams = Helpers.SetPagerParameters(page, ItemInPage, result);
ViewBag.AllPagesCount = pagingParams.AllPagesCount;
ViewBag.CurrentPageNumber = pagingParams.CurrentPageNumber;
ViewBag.CountOfAllItems = pagingParams.CountOfAllItems.ToMoneyFormat().ToPersianNumber();
result = pagingParams.ListData as List<Book> ?? result;
#endregion
foreach (var item in result)
{
var bookRate = (item.BookRate == null || item.BookRate.Count == 0)
? 0.0
: item.BookRate.Average(q => q.Rate);
model.Result.Add(new SearchBookModel
{
Author = item.Author,
Id = item.Id,
.
.
.
});
}
}
else
{
model.Result = new List<SearchBookModel>();
}
return View(model);
}
When I submit the form I see the following query strings(Notice the duplicate names) :
http://localhost:2817/Search?SearchText=book&Page=2&SearchInBooks=true&SearchInBooks=false&SearchInAuthors=true&SearchInAuthors=false&SearchInTags=true&SearchInTags=false
But it has to be something like this :
http://localhost:2817/Search?SearchText=book&Page=2&SearchInBooks=true&SearchInAuthors=true&SearchInTags=true
How can I fix it ?
Thanks
Html.Checkbox (and the related For... methods) generate a hidden input for false, and the checkbox for true. This is to ensure that model binding works consistently when binding.
If you must get rid of "false" items resulting from the hidden inputs, you'll need to construct the checkbox inputs yourself (using HTML and not the helper).
<input type="checkbox" id="SearchInBooks" name="SearchInBooks">
Why dont your create a Post Method with a matching name to the Get method. This will ensure that the code is much easier to debug. As you will not have a huge function to go through trying to find problems like this.
I cannot find a where your getting the duplicate url query strings from though.
This will also allow you to bind your results back to the model.
If you want the model binding to happen successfully then you have to go with this way because that is the nature of the Html.CheckBox/Html.CheckBoxFor methods they will render a hidden field as well.
I would suggest rather go with POST to make your life easy. If you still want to use GET then you have to use checkbox elements directly but you have to take care of the model binding issues. Not all the browsers returns "true" when the checkbox is checked for ex. firefox passes "on" so the default model binder throws an error.
Other alternate options is you can go for custom model binder or you can submit the form using jquery by listening to the submit event.