我对MVC很陌生,在提交表单并让控制器获取发布的值时遇到了一些麻烦。
似乎正在发生的事情是,当表单确实发布到控制器中的正确方法时,传递的模型充满了空值 - 好像它没有被表单填充。
我尝试以与默认Login控件相同的方式创建它,但我显然在某处遗漏了某些东西。任何人都可以放弃任何光明吗?
我的代码如下:
MODEL
Public Class ContactUsDetails
Private _name As String
Private _email As String
Private _details As String
Public ReadOnly Property Name() As String
Get
Return _name
End Get
End Property
Public ReadOnly Property Email() As String
Get
Return _email
End Get
End Property
Public ReadOnly Property Details() As String
Get
Return _details
End Get
End Property
Public Sub New(ByVal name As String, ByVal email As String, ByVal details As String)
_name = name
_email = email
_details = details
End Sub
Public Sub New
End Sub
End Class
查看
@ModelType TestMVC.ContactUsDetails
@Code
ViewData("Title") = "ContactUs"
End Code
@Using Html.BeginForm()
@<fieldset>
<legend>Contact Us</legend>
<div class="editor-label">
@Html.LabelFor(Function(m) m.Name)
</div>
<div class="editor-field">
@Html.TextBoxFor(Function(m) m.Name)
</div>
<div class="editor-label">
@Html.LabelFor(Function(m) m.Email)
</div>
<div class="editor-field">
@Html.TextBoxFor(Function(m) m.Email)
</div>
<div class="editor-label">
@Html.LabelFor(Function(m) m.Details)
</div>
<div class="editor-field">
@Html.TextBoxFor(Function(m) m.Details)
</div>
<p>
<input type="submit" value="Submit" />
</p>
</fieldset>
End Using
CONTROLLER
Namespace TestMVC
Public Class FormsController
Inherits System.Web.Mvc.Controller
'
' GET: /Forms
Public Function ContactUs() As ActionResult
Return View()
End Function
<HttpPost()> _
Public Function ContactUs(model As ContactUsDetails) As ActionResult
If ModelState.IsValid Then
End If
Return View(model)
End Function
End Class
End Namespace
答案 0 :(得分:1)
我对VB不太专业,但我的模型应该具有可编辑的属性,看看你的代码看起来你的模块是只读的。因此模型绑定器无法填写值
答案 1 :(得分:1)
模型绑定器不会通过调用构造函数来填充模型,而是通过设置属性值。因此,您的模型属性不能是我的只读。
答案 2 :(得分:0)
MODEL:
public class FileSetViewModel
{
public int FileId { get; set; }
[DisplayName("From Policy")]
public string FromPolicy { get; set; }
[DisplayName("Policy location")]
public string PolicyLocation { get; set; }
[DisplayName("Policy type")]
public string PolicyType { get; set; }
[DisplayName("File name")]
public string FileName { get; set; }
[DisplayName("Device Type")]
public string DeviceType { get; set; }
}
public class FileSetListViewModel
{
public List<FileSetViewModel> FileSetList { get; set; }
}
查看:
@section DeviceContent {
<h2>File set</h2>
@if (Model.FileSetList.Count() > 0)
{
<table>
<caption>Files loaded on current device</caption>
<thead>
<tr>
<th scope="col">From Policy</th>
<th scope="col">Policy Location</th>
<th scope="col">Policy Type</th>
<th scope="col">File Name</th>
<th scope="col">Device Type</th>
<th scope="col">View</th>
</tr>
</thead>
<tbody>
@foreach (var fileSet in Model.FileSetList)
{
<tr>
<td>@fileSet.FromPolicy</td>
<td>@fileSet.PolicyLocation</td>
<td>@fileSet.PolicyType</td>
<td>@fileSet.FileName</td>
<td>@fileSet.DeviceType</td>
<td><a href="#" onclick="popitup('viewer/@fileSet.FileId');">View</a></td>
</tr>
}
</tbody>
</table>
}
}
控制器:
[HttpGet]
public ActionResult Index(int id)
{
FileSetListViewModel model = _policiesLogic.GetFilesSetForDevice(id);
return View(model);
}
[HttpPost]
public ActionResult Index(FileSetListViewModel model)
{
// preconditions
if (null == model) throw new ArgumentNullException("model");
if (ModelState.IsValid)
{
// Do stuff
}
else // Validation error, so redisplay data entry form
{
return View(model);
}
}
即使模型是空的,我总是将一个实例传递给视图,虽然我可能错了,因为这是我的第一个mvc项目...