简体   繁体   English

MVC数据在视图中传递

[英]MVC data passing in view

I am making my MVC application. 我正在制作我的MVC应用程序。 I open my view with predefined parameters like this: 我用预定义的参数打开我的视图,如下所示:

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

And this works fine, the data subject_id and qty are passed correctly. 这工作正常,数据subject_idqty正确传递。 However, my view PickGroupForHomework contains a form to fill, which is then validated. 但是,我的视图PickGroupForHomework包含要填充的表单,然后进行验证。 If the input is not valid, the window simply should reload with the data subject_id and qty as defined in previous view. 如果输入无效,则窗口应该使用上一视图中定义的数据subject_idqty重新加载。 I do this in such way: 我是这样做的:

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)
            {

            }
            else
            {
                return View(model);
            }
            return View(model);

        }

But the resulting URL does not contain the data I need, but just a plain view. 但是生成的URL不包含我需要的数据,只是一个简单的视图。 How do I do it right? 我该怎么做?

In order for you wep app to work, you will need two actions, one to set your model up for the View and another to actually do the work to post and save your data: 为了让你运行wep app,你需要两个动作,一个是为View设置你的模型,另一个是为了发布和保存你的数据而实际做的工作:

    public ActionResult PickGroupForHomework(int subject_id, int qty)
    {
        //Initialize your model here. Below is just an example.
        ClassDeclarationsDBEntities2 entities = new ClassDeclarationsDBEntities2();

        PickGroupForHomeworkViewModel model = new PickGroupForHomeworkViewModel();

        model.groups = entities.Groups.ToList();
        model.users = entities.Users.ToList();

        model.subject_id = subject_id;
        model.qty = qty;

        return View("PickGroupForHomework", model);
    }

    [HttpPost]
    public ActionResult PickGroupForHomework(PickGroupForHomeworkViewModel model)
    {
        ClassDeclarationsDBEntities2 entities = new ClassDeclarationsDBEntities2();

        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)
        {
            //Save to database
            [code goes here]

            //return to a View to show your results
            return View("[Your view to see the results]")
        }

        //Model Validation did not pass
        //or exception occurred go back to View
        return View(model);
    }

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

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