MVC从视图和模型中保存数据

时间:2017-12-19 17:33:28

标签: asp.net-mvc asp.net-mvc-4 entity-framework-6

我目前正在尝试创建一个向用户询问一组问题的应用程序。我希望应用程序从视图中保存数据,一旦用户单击下一个按钮,它就会保存数据并显示下一个问题。由于我是MVC的新手,我真的很想弄清楚事情。

我希望问题表中的问题加载到UnRegistered视图中。然后我想在RegistrationQuestions表中保存问题Id和答案以及时间戳,会话中的UserID,答案等等。简而言之,我想从QuestionsManager表中选择然后插入到RegistrationQuestions表中。

我对自己的观点感到困惑,因为我花了这么长时间使用ASP.NET ...

型号:

public class RegistrationQuestionsContext : ApplicationDbContext
{
    public DbSet<RegistrationQuestions> RegistrationDetails { get; set; }
    public DbSet<QuestionsManager> QuestionDetails { get; set; }
}

/// <summary>
/// This tables stores the result of the questions asked
/// </summary>
[Table("RegistrationQuestions")]
public class RegistrationQuestions
{
    [Key]
    public int Id { get; set; }
    public string fldUserID { get; set; }
    public int fldQuestionId { get; set; }             
    public string fldAnswer { get; set; }
    public string fldUOM { get; set; }
    public DateTime fldTimeStamp { get; set; }
    public int fldCurrentPosition { get; set; }
    public bool fldRegistrationCompleted { get; set; }       
}

/// <summary>
/// This tables stores questions that are to be asked
/// </summary>
[Table("Questions")]
public class QuestionsManager
{
    [Key]
    public int Id { get; set; }
    public string fldQuestion { get; set; }
    public int fldOptionId { get; set; }
    public bool fldQuestionActive { get; set; }
}

控制器:

   [Authorize]   
    public class RegistrationQuestionsController : Controller
    {
    private RegistrationQuestionsContext db = new RegistrationQuestionsContext();        

    public RegistrationQuestionsController()
    {
        //            
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult UnRegistered()
    {
        //Determines whether registration questions have all been completed
        var Progress = (from o in db.RegistrationDetails.ToList() where o.fldUserID == User.Identity.GetUserId() select (bool?) o.fldRegistrationCompleted);
            bool RegistrationCompleted = Progress.First().Value;

            //If the user has not completed the registration we jump into here
            if (!RegistrationCompleted)
            {
                //This detmerines the current question number that the user is at. This is so if they bail out we know where they were if they log in again.
                var Position = (from o in db.RegistrationDetails.ToList() where o.fldUserID == User.Identity.GetUserId() select (int?) o.fldCurrentPosition);
                int CurrentPosition = Position.First().Value;

                //I want this Question to appear on the View
                var Question = (from o in db.QuestionDetails.ToList() where o.Id == CurrentPosition select o.fldQuestion);
                string CurrentQuestion = Question.First().ToString();

                if (ModelState.IsValid)
                {   
                    //I then want to save the details from the view along with further details from the RegistrationQuestions model in a different table.
                    db.SaveChanges();
                    return RedirectToAction("/RegistrationQuestions/UnRegistered/");
                }

                return Redirect("/RegistrationQuestions/UnRegistered/");
            }
            else
            {
                return Redirect("/RegistrationQuestions/Registered");

            }          
    }        

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }
}

视图:

@model  Foodsnap.Models.QuestionsManager

@{
    ViewBag.Title = "Registration Questions";
}

<!DOCTYPE html>

<html>
<head>
 <meta name="viewport" content="width=device-width" />
 <title></title>
</head>
<body>
 <div>
    You have not completed the registration, please click the button to continue.
    </br>
    YOU ARE UNREGISTERED!!!
</div>
<div class="form-group">
    @Html.LabelFor(m => m.fldQuestion, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(m => m.fldQuestion, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(m => m.fldQuestion, "", new { @class = "text-danger" })
    </div>
</div>    
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Next Question" class="btn btn-default" />
    </div>
</div>

0 个答案:

没有答案
相关问题