0

我对 MVC 很陌生,在提交表单和让控制器获取发布的值时遇到了一些麻烦。

似乎正在发生的事情是,虽然表单确实发布到控制器中的正确方法,但传递的模型充满了空值 - 好像它没有被表单填充。

我尝试以与默认登录控件相同的方式创建它,但我显然在某处遗漏了一些东西。任何人都可以解释一下吗?

我的代码如下:

模型

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

控制器

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
4

3 回答 3

1

我对 VB 不太熟悉,但我的模型应该具有可编辑的属性,看看你的代码,你的模型似乎是只读的。所以模型绑定器无法填写值

于 2013-05-09T10:05:00.203 回答
1

模型绑定器不通过调用构造函数填充模型,而是通过设置属性值。因此,您的模型属性不能是我只读的。

于 2013-05-09T10:08:08.520 回答
0

模型:

  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 项目......

于 2013-05-09T10:21:56.883 回答