asp.net-mvc-3 - MVC,在 Controller 和 View 之间传递类

标签 asp.net-mvc-3

我有一个场景如下; “调查”包含“问题”类型的列表; “问题”由一个 id 和一个名为“Text”的字符串组成。以下是类定义:

public class Survey
{
    public int SurveyId { get; set; }
    public string Title { get; set; }
    public List<Question> Questions { get; set; }

    public Survey()
    {
        this.Questions = new List<Question>();
    }
}

public class Question
{
    public int QuestionId { get; set; }
    public string QuestionText { get; set; }
    public int OwningSurveyId { get; set; }
}

我希望能够从特定调查的页面链接以创建新问题(工作正常),但是当提交问题表单时,我想做两件事; 1) 确保正确设置 OwningSurveyId,以及 2) 将问题添加到 Survey.Questions List。

我对获得此功能的任何解决方案持开放态度 - 我采取的方法可能不是最好的。我决定做的是将调查对象传递到问题的创建表单中,以便在提交表单时,表单包含两个对象,因此我可以正确执行上述(1 和 2)两个任务。

我在 QuestionController 中设置了 Create Question 页面,如下所示:
// GET: /Question/Create

    public ActionResult Create(Survey OwningSurvey)
    {
        return View(OwningPulse);
    }

[HttpPost]
    public ActionResult Create(Pulse OwningPulse, Question NewQuestion)
    {
        ModelState["OwningSurveyId"].Errors.Clear();

        if (ModelState.IsValid)
        {
            OwningPulse.Questions.Add(NewQuestion);
            db.Questions.Add(NewQuestion);
            db.SaveChanges();
            return RedirectToAction("Index", "Survey");
        }

        return View(OwningSurvey);
    }

问题的 Create.cshtml 文件具有以下内容:
@model MoodTool.Interface.Models.Survey

@{
ViewBag.Title = "Create";

var Question = new MoodTool.Interface.Models.Question();
Question.OwningPulseId = Model.SurveyId;
}

@using (Html.BeginForm("Create", "Question", "{NewQuestion}", FormMethod.Post, new { NewQuestion = Question }))
{... code follows

我遇到的问题是 .cshtml 文件中“模型”的值为空,因此页面崩溃。问题 Controller (“GET”版本)的 Create 方法中的 OwningSurvey 的值也为空。

任何人都可以指导我朝着正确的方向前进吗?我意识到我可能在做一些严重错误的事情(设计模式),或者犯了一个简单的语法错误,但我是 MVC 的新手并且一直在兜圈子。

谢谢

最佳答案

您是否正在为在“调查操作”中添加问题的调查加载模型?如果不是,那么这就是您的模型为空的原因。这只是我根据我在这里看到的猜测。 RedirectToActions 不加载模型,因此您必须将其放入 TempData 并在重定向操作中重新加载它。

第二部分

在第一部分中,您将创建问题并重定向到调查的索引操作。

 OwningPulse.Questions.Add(NewQuestion);
 db.Questions.Add(NewQuestion);
 db.SaveChanges();
 return RedirectToAction("Index", "Survey");

进入索引操作后,您需要重新加载您首先放置问题的调查,并将其加载到模型中以传递给 View 。使用上面的 lavriks 方法,将 id 传递给 action 方法,然后您可以从数据库中获取该调查并将其传递给为 View 定义的模型中的 View 。
    public ActionResult Index(int id)
    {
         // assuming you have some built in functionality to retrieve the survey information from the db through the constructor given an id
        Survey s1 = new survey(id);  

        // some of this is pseudo code, assumes you have a model defined called survey for the view using it
        // Return view with model.
        return View( "Survey" , s1 );
    }

当我谈到临时数据而不是将 ID 传递给操作方法并在 View 的操作方法中再次查找时,您可以将对象存储在临时数据中
     // save data to survey in teh create action method
     TempData["survey"] = survey

然后在调查的默认索引操作方法中从临时数据中调用调查并将其发送到 View 。
     Survey s1 = (Survey)Tempdata["survey"];
     return View ("Survey", s1);

同样,其中一些是伪代码,并不完全完整,但它应该为您指明正确的方向。这两种方法都有其优缺点,但取决于应用程序、对象的大小、点击页面的人数等,您可以选择或选择。

关于asp.net-mvc-3 - MVC,在 Controller 和 View 之间传递类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12746037/

相关文章:

ajax - 部分 View 中的 Asp.net MVC WebGrid 未通过 Ajax 更新

asp.net-mvc - 请求验证 token 不匹配

visual-studio-2010 - 文件夹未使用 VS 发布/Web 部署上传

asp.net-mvc - RenderAction调用错误的操作方法

asp.net-mvc - 如何从mvc3调用存储过程?

visual-studio-2010 - Visual Studio 2010 中的 Razor 模板编辑 : why all the type inference errors?

c# - 在没有 CodeFirst 的情况下访问 C#/.NET 中的数据库

asp.net-mvc-3 - 指定 Html.TextBoxFor 的大小和最大长度

c# - MVC 模型绑定(bind) + 格式化

ajax - mvc3 - ajax表单提交和服务器端验证