我在这里有4个模型类,我已经在html表单中提取问卷字段,问题和答案选项,我有一个控制器操作方法,我想发布这个问卷数据并保存响应和问卷详细信息。有人可以帮我吗?
public class Question
{
public long? ParentId { get; set; }
public long Id { get; set; }
public long OrderNumber { get; set; }
public string Text { get; set; }
public List<AnswerOption> AnswerOptions { get; set; }
}
public class AnswerOption
{
public long Id { get; set; }
public long QuestionId { get; set; }
public string Option { get; set; }
}
public class Response
{
public long Id { get; set; }
public long QuestionnaireId { get; set; }
public long QuestionId { get; set; }
public long AnswerId { get; set; }
public string AnswerText { get; set; }
public DateTime? CreatedOn { get; set; }
public AnswerOption AnswerOption { get; set; }
public Question Question { get; set; }
}
public class Questionnaire
{
public long Id { get; set; }
public string Email { get; set; }
public string BusinessName { get; set; }
public string DBA { get; set; }
public string MailingAddress { get; set; }
public string MailingCity { get; set; }
public string MailingState { get; set; }
public string MailingZip { get; set; }
public DateTime? CreatedOn { get; set; }
public string Signature { get; set; }
public string Title { get; set; }
public List<Response> Responses { get; set; }
}
这是Html,
@{ var questions = GetAllQuestions();
}
<form name="updateForm" asp-controller="Questionnaire" asp-action="SubmitApplication" id="updateForm" asp-return-route="@ViewData["ReturnUrl"]" method="post" class="form-horizontal">
<div class="panel panel-midnightblue">
<div class="panel-heading">
<h2>Questionnaire Information</h2>
</div>
<div class="panel-body">
<div class="alert alert-info">
<div class="row horizontal-nav">
<div class="col-xs-4">
<i class="fa fa-info-circle fa-stack-1x"></i>
</div>
<div class="col-xs-8">
<strong>Please enter your information here.</strong>
</div>
</div>
</div>
<hr />
<span class="grid__col-4">
<label class="text-large mb0 alignleft">Business Name</label>
<input class="form-control" name="BusinessName" />
</span>
<span class="grid__col-3">
<label class="text-large mb0">Email Address</label>
<input class="form-control" name="Email" />
</span>
<span class="grid__col-3">
<label class="text-large mb0">DBA</label>
<input class="form-control" name="DBA" />
</span>
<span class="grid__col-4">
<label class="text-large mb0 alignleft">Mailing Address</label>
<input class="form-control" name="MailingAddress" />
</span>
<span class="grid__col-3">
<label class="text-large mb0">City</label>
<input class="form-control" name="MailingCity" />
</span>
<span class="grid__col-2">
<label class="text-large mb0">State</label>
<input class="form-control" name="MailingState" />
</span>
<span class="grid__col-2">
<label class="text-large mb0">Zip</label>
<input class="form-control" name="MailingZip" />
</span>
<span>
<input class="form-control hidden" name="CreatedOn" value="@DateTime.Now"/>
</span>
<div class="grid__col-2">
<label class="text-large mb0">Signature</label>
<input class="form-control" name="Signature"/>
</div>
<span class="grid__col-2">
<label class="text-large mb0">Title</label>
<input class="form-control" name="Title" />
</span>
@for (var i = 0; i < questions.Count; i++)
{
<span class="grid__col-2">
<label class="text-large mb0">@questions[i].Text</label>
<input type="hidden" name="Responses[@i].QuestionId" value="@questions[i].Id"/>
<input type="hidden" name="Responses[@i].Id" value="@i"/>
@foreach (var answerOption in questions[i].AnswerOptions)
{
if (answerOption.Option != null)
{
<span>
<input type="radio" value="@answerOption.Option" name="@answerOption.QuestionId"/>
<label>@answerOption.Option</label>
</span>
}
else if (answerOption.Option == null)
{
<span>
<input type="text" id="@answerOption.QuestionId" value=""/>
</span>
}
}
</span>
}
</div>
<div class="panel-footer">
<div class="form-group center">
<button id="submitInfo" type="submit" class="btn btn-info">Submit Application</button>
</div>
</div>
</div>
</form>
这是我的控制器操作方法
public IActionResult SubmitApplication(Questionnaire questionnaire)
{
var successSaveQuestionnaire = Base.RepoService.SaveQuestionnaire(questionnaire);
return RedirectToAction("Index", successSaveQuestionnaire ? new { Message = FeedbackMessageEnum.SaveQuestionnaireSuccessEnum } : new { Message = FeedbackMessageEnum.SaveQuestionnaireFailedEnum });
}