Asp.net mvc Razor中的绑定返回null(OnPostAsync)
在下面的代码中,Asp.net mvc Razor中的绑定返回null(OnPostAsync),asp.net-mvc,razor,razor-pages,Asp.net Mvc,Razor,Razor Pages,在下面的代码中,QuestionViewModel中的所有值都为空。你知道我在装订方面做错了什么吗 cs文件: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetC
QuestionViewModel
中的所有值都为空。你知道我在装订方面做错了什么吗
cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using VerityLearn.DataAccess;
using VerityLearn.Domain;
using VerityLearn.DataAccess.UOW;
using VerityLearn.WebUI.ViewModels;
namespace VerityLearn.WebUI.Pages.Questions
{
[Authorize]
public class StudentQuestionsModel : PageModel
{
private readonly SignInManager<VerityUser> _signInManager;
private readonly UserManager<VerityUser> _userManager;
private readonly IStudentQuesionsUOW _studentQuesionsUOW;
private readonly VerityLearn.DataAccess.VerityContext _context;
public StudentQuestionsModel(
VerityContext context,
SignInManager<VerityUser> signInManager,
UserManager<VerityUser> userMrg,
IStudentQuesionsUOW studentQuesionsUOW
)
{
_context = context;
_signInManager = signInManager;
_userManager = userMrg;
_studentQuesionsUOW = studentQuesionsUOW;
} // end public StudentQuestionsModel(VerityContext context, SignInManager<VerityUser> signInManager, UserManager<VerityUser> userMrg)
#region User Properties
public VerityUser VerityUser { get; set; }
//[TempData]
//public string UserId { get; set; }
public Student Student { get; set; }
public Prospect Prospect { get; set; }
public Parent Parent { get; set; }
public ExamUserViewModel ExamUserViewModel { get; set; }
#endregion // User Properties
public DateTime DateStarted { get; set; }
public Exam Exam { get; set; }
[TempData]
public int ExamId { get; set; }
ExamQuestion ExamQuestion { get; set; }
public List<ExamQuestion> ExamQuestions { get; set; }
[TempData]
public int NbrOfExamQuestions { get; set; }
public ExamViewModel ExamViewModel { get; set; }
[TempData]
public int QuestionNdx { get; set; }
[BindProperty]
public QuestionViewModel QuestionViewModel { get; set; }
[ViewData]
public string Message { get; set; }
[BindProperty]
public Question Question { get; set; }
public async Task OnGetAsync(int? examId, int? questionNdx)
{
Message = string.Empty;
if (_signInManager.IsSignedIn(HttpContext.User))
{
string email = HttpContext.User.Identity.Name;
VerityUser = await _studentQuesionsUOW.GetVerityUser(email);
//UserId = VerityUser.Id.ToString();
// TODO: Setup priorities of setting Student, Prospect and Parent properties, might involve Enrollments 4/30/2020
//Student = await _context.Students.Where(s => s.UserId == VerityUser.Id).FirstOrDefaultAsync<Student>();
//Prospect = await _context.Prospects.Where(p => p.UserId == VerityUser.Id).FirstOrDefaultAsync<Prospect>();
//Parent = await _context.Parents.Where(p => p.UserId == VerityUser.Id).FirstOrDefaultAsync<Parent>();
DateStarted = DateTime.Now;
if ((examId != null) && (examId.Value > 0))
{
ExamId = examId.Value;
Exam = await _context.Exams.Where(e => e.ExamId == examId)
.Include(e => e.Course).ThenInclude(c => c.Subject).FirstOrDefaultAsync<Exam>();
if (Exam != null)
{
ExamUserViewModel = new ExamUserViewModel
{
ExamUserId = 0,
ExamId = Exam.ExamId,
TimeStarted = DateTime.Now,
Status = ExamUserStatus.InProgress,
StudentId = VerityUser.StudentId,
ProspectId = VerityUser.ProspectId,
ParentId = VerityUser.ParentId
};
// TODO: If this is a new ExamUser, we must insert it to VerityLearnDB2.ExamUsers
if (NbrOfExamQuestions == 0)
{
ExamQuestions = await _context.ExamQuestions
.Where(eq => eq.ExamId == examId)
.ToListAsync<ExamQuestion>();
NbrOfExamQuestions = ExamQuestions.Count;
TempData.Keep("NbrOfExamQuestions");
} // endif (NbrOfExamQuestions == 0)
if ((questionNdx == null) || (questionNdx.Value == 0))
{
questionNdx = 1;
} // endif ((questionNdx == null) || (questionNdx.Value == 0))
QuestionNdx = questionNdx.Value;
TempData.Keep("QuestionNdx");
ExamQuestion = await _context.ExamQuestions
.Include(eq => eq.Question)
.ThenInclude(q => q.Options)
.Where(eq => eq.ExamQuestionOrder == questionNdx)
.FirstOrDefaultAsync<ExamQuestion>();
QuestionViewModel = new QuestionViewModel
{
QuestionId = ExamQuestion.QuestionId,
ExamQuestionOrder = ExamQuestion.ExamQuestionOrder,
QuestionText = ExamQuestion.Question.QuestionText,
IsSingleSelection = ExamQuestion.Question.IsSingleSelection,
Options = new List<OptionViewModel>()
};
ExamViewModel = new ExamViewModel
{
ExamId = Exam.ExamId,
ExamName = Exam.ExamName,
Questions = new List<QuestionViewModel>()
};
ExamViewModel.Questions.Add(QuestionViewModel);
ExamViewModel.ExamUserViewModel = ExamUserViewModel;
List<AnswerOption> answerOptions = _context.AnswerOptions
.Where(ao => ao.QuestionId == ExamQuestion.QuestionId)
.ToList<AnswerOption>();
foreach (AnswerOption ao in answerOptions)
{
OptionViewModel ovm = new OptionViewModel
{
OptionId = ao.OptionId,
OptionText = ao.OptionText,
IsCorrect = ao.IsCorrect
};
ovm.UserExamOptionViewModel = new UserExamOptionViewModel
{
UserExamOptionId = ExamUserViewModel.ExamUserId,
UserExamId = 0,
OptionId = ao.OptionId,
IsSelected = false
};
QuestionViewModel.Options.Add(ovm);
}
}
else
{
Message = String.Format("Error: Exam with Identifier, {0}, was not found.", examId);
}
}
else
{
Message = String.Format("Error: Exam with Identifier, {0}, was not found.", examId);
}
}
else
{
Message = "Error: Login is required.";
}
}
public async Task<IActionResult> OnPostAsync(QuestionViewModel QuestionViewModel)
{
var t = QuestionViewModel;
if (!ModelState.IsValid)
{
return Page();
}
_context.Attach(Question).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
}
return RedirectToPage("./Index");
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Authorization;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.AspNetCore.Mvc.RazorPages;
使用Microsoft.EntityFrameworkCore;
使用Microsoft.AspNetCore.Identity;
使用VerityLearn.DataAccess;
使用VerityLearn.Domain;
使用VerityLearn.DataAccess.UOW;
使用VerityLearn.WebUI.ViewModels;
命名空间VerityLearn.WebUI.Pages.Questions
{
[授权]
公共课堂学生问题模型:页面模型
{
专用只读签名管理器\u签名管理器;
私有只读用户管理器_UserManager;
私人只读是学生提问提问;
私有只读VerityLearn.DataAccess.VerityContext\u上下文;
公共学生问题模型(
VerityContext上下文,
SignInManager SignInManager,
UserManager userMrg,
学生提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问提问
)
{
_上下文=上下文;
_signInManager=signInManager;
_userManager=userMrg;
_studentquesinosuow=studentquesinosuow;
}//结束公共学生问题模型(VerityContext上下文、SignInManager SignInManager、UserManager userMrg)
#区域用户属性
公共VerityUser VerityUser{get;set;}
//[临时数据]
//公共字符串用户标识{get;set;}
公立学生学生{get;set;}
公共前景前景{get;set;}
公共父级{get;set;}
公共ExamUserViewModel ExamUserViewModel{get;set;}
#endregion//用户属性
公共日期时间日期开始{get;set;}
公开考试{get;set;}
[临时数据]
公共int ExamId{get;set;}
ExamQuestion ExamQuestion{get;set;}
公共列表ExamQuestions{get;set;}
[临时数据]
public int NbrOfExamQuestions{get;set;}
公共ExamViewModel ExamViewModel{get;set;}
[临时数据]
公共整数问题ndx{get;set;}
[BindProperty]
公共问题视图模型问题视图模型{get;set;}
[查看数据]
公共字符串消息{get;set;}
[BindProperty]
公共问题{get;set;}
公共异步任务OnGetAsync(int?examId,int?questionNdx)
{
Message=string.Empty;
if(_signInManager.IsSignedIn(HttpContext.User))
{
字符串email=HttpContext.User.Identity.Name;
VerityUser=wait_studentquesonsuow.GetVerityUser(电子邮件);
//UserId=VerityUser.Id.ToString();
//TODO:设置学生、潜在客户和家长属性的优先级,可能涉及2020年4月30日的注册
//Student=wait_context.Students.Where(s=>s.UserId==VerityUser.Id).FirstOrDefaultAsync();
//Prospect=wait_context.Prospects.Where(p=>p.UserId==VerityUser.Id).FirstOrDefaultAsync();
//Parent=wait_context.Parents.Where(p=>p.UserId==VerityUser.Id).FirstOrDefaultAsync();
DateStarted=DateTime.Now;
如果((examId!=null)&&(examId.Value>0))
{
ExamId=ExamId.值;
考试=等待_context.Exams.Where(e=>e.ExamId==ExamId)
.Include(e=>e.Course)。然后Include(c=>c.Subject)。FirstOrDefaultAsync();
如果(检查!=null)
{
ExamUserViewModel=新ExamUserViewModel
{
ExamUserId=0,
ExamId=Exam.ExamId,
TimeStarted=DateTime。现在,
状态=ExamUserStatus.InProgress,
StudentId=VerityUser.StudentId,
ProspectId=VerityUser.ProspectId,
ParentId=VerityUser.ParentId
};
//TODO:如果这是一个新的ExamUser,我们必须将其插入VerityLearnDB2.ExamUsers
如果(NbrOfExamQuestions==0)
{
ExamQuestions=wait_context.ExamQuestions
其中(eq=>eq.ExamId==ExamId)
.ToListAsync();
NbrOfExamQuestions=ExamQuestions.Count;
TempData.Keep(“NbrOfExamQuestions”);
}//endif(NbrOfExamQuestions==0)
if((questionNdx==null)| |(questionNdx.Value==0))
{
问题ndx=1;
}//endif((questionNdx==null)| |(questionNdx.Value==0))
QuestionNdx=QuestionNdx.值;
TempData.Keep(“问题ndx”);
ExamQuestion=wait_context.ExamQuestions
.包括(等式=>等式问题)
.然后包括(q=>q.Options)
其中(eq=>eq.ExamQuestionOrder==questiondx)
.FirstOrDefaultAsync();
QuestionViewModel=新的QuestionViewModel
{
QuestionId=ExamQuestion.QuestionId,
ExamQuestionOrder=ExamQuestion.ExamQuestionOrder,
QuestionText=ExamQuestion.Question.QuestionText,
IsSingleSelection=ExamQuestion.Question.IsSingleSelection,
选项=新列表()
};
@page "{examId:int?}"
@model VerityLearn.WebUI.Pages.Questions.StudentQuestionsModel
@{
ViewData["Title"] = "StudentQuestions";
}
@if (String.IsNullOrEmpty(@Model.Message))
{
<div class="row" style="background-color: #5D2685; color: #FFFF80;">
<div class="col-md-6">
<h4>Exam: @Html.DisplayFor(model => model.Exam.ExamName)</h4>
</div>
<div class="col-md-6">
<h4>Course: @Html.DisplayFor(model => model.Exam.Course.CourseName)</h4>
</div>
</div>
<br />
<br />
<div>
<h4>Question</h4>
<hr />
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Question.QuestionId" />
<div class="form-group">
<label asp-for="Question.QuestionText" class="control-label"></label>
<input asp-for="Question.QuestionText" class="form-control" />
<span asp-validation-for="Question.QuestionText" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Question.KeyToAnswer" class="control-label"></label>
<input asp-for="Question.KeyToAnswer" class="form-control" />
<span asp-validation-for="Question.KeyToAnswer" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Question.IsSingleSelection" class="control-label"></label>
<input asp-for="Question.IsSingleSelection" class="form-control" />
<span asp-validation-for="Question.IsSingleSelection" class="text-danger"></span>
</div>
<label asp-for="QuestionViewModel.QuestionText" class="control-label"></label>
@if (Model.QuestionViewModel.IsSingleSelection.Value)
{
<p>Select the correct option.</p>
@foreach (OptionViewModel opt in Model.QuestionViewModel.Options)
{
<input type="radio" name="option" value="@opt.UserExamOptionViewModel.IsSelected"><label for=" @opt.OptionText"> @opt.OptionText </label><br />
}
}
else
{
<p>Select the correct options (More than one).</p>
@foreach (OptionViewModel opt in Model.QuestionViewModel.Options)
{
<input type="checkbox" name="option" value="@opt.UserExamOptionViewModel.IsSelected"><label for=" @opt.OptionText"> @opt.OptionText </label><br />
}
}
@{
var prevDisabled = (Model.QuestionNdx <= 1) ? "disabled" : "";
var nextDisabled = (Model.QuestionNdx >= Model.NbrOfExamQuestions) ? "disabled" : "";
}
<button type="submit" asp-route-questionIndex="@(Model.QuestionNdx - 1)" class="btn btn-primary @prevDisabled">Previous</button>
<button type="submit" asp-route-questionIndex="@(Model.QuestionNdx + 1)" class="btn btn-primary @nextDisabled">Next</button>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
}
else
{
<div class="row" style="background-color: #5D2685; color: #FFFF80;">
<div class="col-md-6">
<h4>@Model.Message</h4>
</div>
</div>
}
@foreach(OptionViewModel opt in Model.QuestionViewModel.Options)
{
optionIndex++;
@Html.RadioButtonFor(model => model.QuestionViewModel.Options[optionIndex].OptionText, @opt.OptionText);
@opt.OptionText<br />
}
public class Subject
{
public string SubjectName { get; set; }
}
public class Address
{
public string StreetAddress { get; set; }
}
public class Student
{
public string Name { get; set; }
public Address HomeAddress { get; set; }
public List<Subject> Subjects { get; set; }
}
@Html.TextBoxFor(model => model.Name)
<input id="Name" name="Name" type="text" value="John Smith">
@Html.TextBoxFor(model => model.HomeAddress.StreetAddress)
<input id="HomeAddress_StreetAddress" name="HomeAddress.StreetAddress" type="text" value="some address">
@Html.TextBoxFor(model => model.Subjects[0].SubjectName)
<input id="Subjects_0__SubjectName" name="Subjects[0].SubjectName" type="text" value="Math">
<input type="checkbox" name="option" value="@opt.UserExamOptionViewModel.IsSelected">