输入在MVC和Razor中不起作用的所选复选框 [英] Inputting selected checkboxes not working in MVC and Razor

查看:134
本文介绍了输入在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

推荐答案

只需在复选框中输入相同的名称,就像下面应该使用的那样。



Just give the same name to the checkbox like for all you should use below.

<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键。



如下所示< br $>




Now when you are submitting the page, just use the FormCollection and use the key "chkName".

Like below

public 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.....


做你正在做的最好的方法是使用javascript构建答案列表并制作一个AJAX呼叫。您可以使用jquery获取已检查的id列表,然后将其构建为JSON字符串并将其传递通过。如果您不愿意,csv可以使用或者XML,则不需要使用JSON。
The nicest way to do what you're doing, is to build the list of answers using javascript and make an AJAX call. You can use jquery to get the list of checked ids, then build that in to a JSON string and pass it through. You don't need to use JSON if you prefer not to, a csv works, or XML.


这篇关于输入在MVC和Razor中不起作用的所选复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆