本文介绍了输入在MVC和Razor中不起作用的所选复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我无法创建一个数组来输入数据库。这很可能是我的语法,因为这是我的第一个MVC,Razor项目,所以如果你能发现错误并告诉我这将是一个巨大的帮助! 这是我的观点; I am not able to create an array to input into a db. Its most likely my syntax as this is my first MVC, Razor project, so if you can spot the errors and tell me it would be a huge help!This is my view;// Checkbox<div class="checkbox">@{ int count=1; string name="Answer"+1; foreach (var item in q.QuestionOptions.OrderBy(o => o.QuestionOptionRanking)) { if (q.Answer == item.QuestionOption1) { <input type="checkbox" name="@name" id="Answer" value="@item.QuestionOption1" checked />@item.QuestionOption1<br /> q.Answer = q.Answer[count - 1] + "," + item.QuestionOption1; } else { <input type="checkbox" name="@name" id="Answer" value="@item.QuestionOption1" />@item.QuestionOption1<br /> } } count++; }</div> 虽然这似乎循环填充页面,我无法让它创建一个数组来回答输入我的数据库。 这是输入控制器; While this does seem to cycle through to populate the page, I cannot get it to create an array for Answer to input into my db. This is the input controller;// POST: /Questions/ViewQuestion/5[HttpPost][ValidateAntiForgeryToken]public ActionResult ViewQuestion([Bind(Include = "QuestionId, Answer, UserId")] ResponseViewModel responseViewModel){ var page = System.Web.HttpContext.Current.Request["page"]; var myType = System.Web.HttpContext.Current.Request["QuestionTypeId"]; Response re = new Models.Response(); if (myType != "3") { re.Answer = responseViewModel.Answer; } else { for (int i = 1; i < responseViewModel.Answer.Length; i++) { re.Answer = responseViewModel.Answer + ", " + responseViewModel.Answer[i]; } } if (re.Answer == null) { re.Answer = "Work in progress!"; } re.UserId = responseViewModel.UserId; re.QuestionId = responseViewModel.QuestionId; re.Source = "Web"; re.Status = "New"; re.DateStamp = System.DateTime.Now; db.Responses.Add(re); db.SaveChanges(); return RedirectToAction("ViewQuestion/" + page);} 有人能发现我做错了吗? 非常感谢Can anyone spot what I am doing wrong, please?Many thanks推荐答案 <input type="checkbox" id="chkid1" name="chkName" value="1" /><input type="checkbox" id="chkid2" name="chkName" value="2" /><input type="checkbox" id="chkid3" name="chkName" value="3" /> 现在当你提交页面时,只需使用FormCollection并使用chkName键。 如下所示 Now when you are submitting the page, just use the FormCollection and use the key "chkName".Like belowpublic ActionResult PostThisPafe(FormCollection form){ string result = form["chkName"]; return View();} 在该结果中,您将以逗号分隔的方式获取这些值。 Cheers buddy ..... In that result you will get those values in a comma seperated way.Cheers buddy..... 这篇关于输入在MVC和Razor中不起作用的所选复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 17:10