简体   繁体   English

MVC查看数据传递

[英]MVC view data passing

I am making my MVC application. 我正在制作我的MVC应用程序。 My view PickGroupForHomework is redirected to by 我的观点PickGroupForHomework被重定向到

return RedirectToAction("PickGroupForHomework", "Account", new { subject_id = id, qty=model.qty });  

Of course subject_id and qty are parameters of PickGroupForHomeworkViewModel . 当然subject_idqtyPickGroupForHomeworkViewModel参数。 The controller looks like this: 控制器如下所示:

public ActionResult PickGroupForHomework(PickGroupForHomeworkViewModel model)
{
    ClassDeclarationsDBEntities2 entities = new ClassDeclarationsDBEntities2();
    model.groups = entities.Groups.ToList();
    model.users = entities.Users.ToList();
    int id = model.subject_id;
    var subj = entities.Subjects
            .Where(b => b.class_id == model.subject_id)
            .FirstOrDefault();
    if (subj != null)
    {
        model.subject_name = subj.name;
    }
    if (ModelState.IsValid)
    {
        DateTime myDate = DateTime.ParseExact(model.deadline+ " "+model.time, "yyyy-MM-dd HH:mm",
                               System.Globalization.CultureInfo.InvariantCulture);
        ClassDeclarationsDBEntities2 entities2 = new ClassDeclarationsDBEntities2();
        int total = entities2.Tasks.Count();

        for (int i=0;i<model.task_names.Count;i++)
        {
            ClassDeclarationsDBEntities2 entities3 = new ClassDeclarationsDBEntities2();
            int maxid;
            if (total == 0)
            {
                maxid = 0;
            }
            else {
                maxid = entities3.Tasks.Max(u => u.task_id);
            }

            var task = new Models.Task(model.task_names[i], model.subject_id, myDate, model.points[i], maxid + 1);
            entities3.Tasks.Add(task);
            entities3.SaveChangesAsync();
        }
        return RedirectToAction("OperationSuccess", "Account");
    }
    else
    {
        return View(model);
    }
    return View(model);

}

At first, everything loads correctly with correct URL including data passed from previous view. 首先,使用正确的URL(包括从先前视图传递的数据)正确加载所有内容。 The form I am displaying now includes validation. 我现在显示的表单包括验证。 If a user makes mistake in form, which indicades ModelState.IsValid=false , the window is reloaded. 如果用户在形式上出错,则表示ModelState.IsValid=false ,将重新加载窗口。 But I do not know why it is reloaded without data passed from previous window: subject_id and qty . 但是我不知道为什么在没有从前一个窗口传递数据的情况下重新加载它: subject_idqty What am I doing wrong? 我究竟做错了什么?
EDIT: 编辑:

View:   @model ClassDeclarationsThsesis.Models.PickGroupForHomeworkViewModel

@{
    ViewBag.Title = "Pick Group For Homework"; }

<h2>Setting homework for @Model.subject_name</h2>

@foreach (var user in Model.users) {
    if (user.email.Replace(" ", String.Empty) == HttpContext.Current.User.Identity.Name)
    {
        if (user.user_type.Replace(" ", String.Empty) == 2.ToString()|| user.user_type.Replace(" ", String.Empty) == 3.ToString())
        {
            using (Html.BeginForm("PickGroupForHomework", "Account", FormMethod.Post, new { @class = "form-horizontal", enctype = "multipart/form-data" })) {
                @Html.AntiForgeryToken()
                <hr />
                <div class="form-group">
                    @Html.LabelFor(model => model.deadline, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.deadline, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.deadline, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.time, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.time, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.deadline, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.file, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        <div class="editor-field">

                            <input type="file" name="file" />
                        </div>
                    </div>
                </div>
                <div class="form-group">

                    <table>
                        <tr>
                            <th>
                                Name of task
                            </th>
                            <th>
                                Points
                            </th>
                        </tr>

                        @for (int i = 0; i < Model.qty; i++)
                        {
                            <tr>
                                <th>
                                    <div class="form-group">
                                        <div class="col-md-10">

                                            @Html.TextBoxFor(m => m.task_names[i], new { @class = "form-control" })
                                        </div>
                                    </div>

                                </th>
                                <th>
                                    <div class="form-group">
                                        <div class="col-md-10">

                                            @Html.TextBoxFor(m => m.points[i], new { @class = "form-control" })
                                        </div>
                                    </div>
                                </th>
                            </tr>
                        }

                    </table>
                    </div>
                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10">
                            <input type="submit" class="btn btn-default" value="Submit" />
                        </div>
                    </div>
                    }
                    }
                    if (user.user_type.Replace(" ", String.Empty) == 1.ToString() )
                    {
                    <p>You do not have enough permissions to enter this page. Contact the administrator.</p>
                    }

                    }
                    }

Your problem is subject_id is not in your form, so when you post back the form it sends 0 value to the server. 您的问题是subject_id不在您的表单中,因此当您发回表单时,它将向服务器发送0值。

you need to add a field inside form , you can add a Text or hidden field 您需要在表单中添加字段,可以添加文本或隐藏字段

@Html.HiddenFor(m => m.subject_id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM