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

モデル バインダーは、コンストラクターを呼び出すことによってではなく、プロパティ値を設定することによってモデルを設定します。したがって、モデル プロパティは読み取り専用であってはなりません。

于 2013-05-09T10:08:08.520 に答える
1

私はVBの専門家ではありませんが、モデルのプロパティを編集可能にする必要があります。コードを見ると、モデルは読み取り専用のようです。したがって、モデルバインダーは値を入力できません

于 2013-05-09T10:05:00.203 に答える
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 に答える