我的存储过程如下。 此存储过程将数据插入AnswerOptiontbl并将外键插入Correctanswer tbl。
ALTER procedure [dbo].[InsertIntoAnswer]
(
@questionid int,
@OptionA nvarchar(50),
@OptionB nvarchar(50),
@OptionC nvarchar(50),
@OptionD nvarchar(50),
@CorrectOption nvarchar(50)
)
as
begin
insert into Answertbl(QuestionId,OptionA,OptionB,OptionC,OptionD,CorrectOption)
values(@questionid,@OptionA,@OptionB,@OptionC,@OptionD,@CorrectOption)
end
sp.insertcorrectanswer。此存储过程将数据插入CorrectAnswer表。
ALTER procedure [dbo].[InsertCorrectAnswer]
(
@AnswerId int,
@CorrectAnswer nvarchar(50)
)
as
begin
insert into CorrectAnswertbl(AnswerId,CorrectAnswer)
values(@AnswerId,@CorrectAnswer)
end
现在我在以下storedprocedure中定义我的整个逻辑。 sp.insertintoquestion。作为这个sp exec,它会将数据插入到questiontbl,answeroption表和correctanswer tbl中。同时将FK questionid插入Answeroption表并将Fk AnswerId插入CorrectAnswer tbl
ALTER procedure [dbo].[InsertIntoQuestion]
(
@QuestionText nvarchar(50),
@QuestionTypeId int,
@QuestionLevelId int,
@questionid int out,
@OptionA nvarchar(50),
@OptionB nvarchar(50),
@OptionC nvarchar(50),
@OptionD nvarchar(50),
@CorrectOption nvarchar(50),
@AnswerId int out,
@CorrectAnswer nvarchar(50)
)
as
begin
insert into Questiontbl(QuestionText,QuestionTypeId,QuestionLevelId)
values( @QuestionText,@QuestionTypeId,@QuestionLevelId)
select @questionid= scope_identity();
exec InsertIntoAnswer @questionid,@OptionA,@OptionB,@OptionC,@OptionD,@CorrectOption
select @AnswerId=SCOPE_IDENTITY();
exec InsertCorrectAnswer @AnswerId,@CorrectAnswer
end
由于我正在使用DataBase Ist方法,因此在Repositry类中我定义了以下方法,并将参数所需参数传递给storedprocedures。但我不确定我的方法是否正确。
public void InsertQuestions( string QuestionText,int QuestionTypeId, int QuestionLevelId, string OptionA, string OptionB,string OptionC,string OptionD,string CorrectOption,string CorrectAnswer,ObjectParameter questionid=null,ObjectParameter AnswerId=null)
{
db.InsertIntoQuestion(QuestionText, QuestionTypeId, QuestionLevelId, questionid, OptionA, OptionB, OptionC, OptionD, CorrectOption, AnswerId, CorrectAnswer);
}
我在控制器中编写以下代码。 QuestionTypeId和QuestionLevelId是来自QuestionTypetbl和QuestionLeveltbl的questiontbl中的外键。我在选择列表中写这些。在插入过程中,我将从相应的表中选择下拉列表中的这两个。
public class MyScoreController : Controller
{
ScoreApplication app = new ScoreApplication();
scoredbEntities db = new scoredbEntities();
//
// GET: /MyScore/
public ActionResult Index()
{
return View();
}
public ActionResult Create()
{
ViewBag.QuestionTypeId = new SelectList(db.QuestionTypetbls, "QuestionTypeId", "QuestionType");
ViewBag.QuestionLevelId = new SelectList(db.QuestionLeveltbls, "QuestionLevelId", "QuestionLevel");
return View();
}
[HttpPost]
public ActionResult Create()
{
if (ModelState.IsValid)
{
//Now i dont now what to do here.
}
return View();
}
}
以下是视图,但此视图仅包含一个模型,我不知道如何在同一视图中显示其他模型的数据。
@model MyScoreProject.Models.Questiontbl
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Questiontbl</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.QuestionText, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.QuestionText)
@Html.ValidationMessageFor(model => model.QuestionText)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.QuestionTypeId, "QuestionTypeId", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("QuestionTypeId", String.Empty)
@Html.ValidationMessageFor(model => model.QuestionTypeId)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.QuestionLevelId, "QuestionLevelId", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("QuestionLevelId", String.Empty)
@Html.ValidationMessageFor(model => model.QuestionLevelId)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
答案 0 :(得分:0)
我们的想法是拥有不同的视图模型和领域模型。我假设你有一个分层架构。你提到的模型应该在不同的层中。你的模型对应于你的表格。
在MVC项目中,您应该在Models文件夹中拥有视图模型。这些模型基于您需要在视图中显示的数据。这些模型由域模型的不同属性(或计算属性)组成。因此,您可以将此视图模型传递到视图中,并获取与不同域模型(或BusinessObjects。
)相关的数据示例强>
public class MyViewModel{
//some properties from questions to display
//.
//.
//.
//some properties from answer to capture
//.
//.
//.
}
在视图中使用:
@model MyScoreProject.Models.MyViewModel
修改:1(控制器)强>
public ActionResult Create()
{
Question q = bal.GetQuestion(someQuestionParameter);
Answer a = bal.GetAnswer(someAnswerParameter)
MyViewModel m = new MyViewModel(q,a);
return View(m);
}
[HttpPost]
public ActionResult Create(MyViewModel model)
{
if (ModelState.IsValid)
{
//from data of model, construct questions and answer objects and save them in DB
}
return View(m);
}