以下是我的应用程序架构。我对我理解 MVC 是否正确感到困惑?我的架构是对还是错?
视图 - 与用户交互,它有 HTML 部分,提交它调用控制器的表单
控制器 - 它检查添加的信息是否有效?(不是数据库的观点。它只会检查天气是否所有必填字段都已填写?)决定调用哪个模型。
模型 - 它包含视图类。还有一些方法,如添加、修改或删除来处理数据库。
这是正确的还是犯了一些错误?以下是我的代码示例
控制器:
public ActionResult AddCustomer(CustomerModel 模型)
{
如果(模型状态。IsValid)
{
模型.AddCustomer();
return RedirectToAction("Index", "Home");
}
return (View("AddCustomer",model));
}
模型:
公共类 AddBookModel
{
[Required(ErrorMessage = "ISBN 是必需的。")]
[显示名称(“ISBN”)]
公共字符串 ISBN { 获取;放; }
[DisplayName("Title")]
[Required(ErrorMessage = "The Title is required.")]
public String Title { get; set; }
[Required(ErrorMessage = "The Publisher is required.")]
[DisplayName("Publisher")]
public String Publisher { get; set; }
public void AddBook()
{
using (BBBDataContext DCBook = new BBBDataContext())
{
Book tableBook = new Book()
{
ISBN = this.ISBN,
Title = this.Title,
Publisher = this.Publisher,
}
DCBook.Books.InsertOnSubmit(tableBook);
DCBook.SubmitChanges();
}
}
看法:
<% using (Html.BeginForm()) {%>
<%= Html.ValidationSummary(true) %>
<fieldset>
<legend>Insert Book Record</legend>
<table id="displayform" cellspacing="0" cellpadding="5">
<colgroup>
<col span="1" style="text-align:right" />
<col span="2" style="text-align:left" />
</colgroup>
<tr>
<td class="editor-label">
<%= Html.LabelFor(model => model.ISBN) %>
</td>
<td class="editor-field">
<%= Html.TextBoxFor(model => model.ISBN) %>
<%= Html.ValidationMessageFor(model => model.ISBN) %>
</td>
</tr>
<tr>
<td class="editor-label">
<%= Html.LabelFor(model => model.Title) %>
</td>
<td class="editor-field">
<%= Html.TextBoxFor(model => model.Title) %>
<%= Html.ValidationMessageFor(model => model.Title) %>
</td>
</tr>