2017-08-08 9 views
0

私はユーザーの要求に対して小さなASPプロジェクトを持っています。私はASP.NETにとって非常に新しいです。私はちょうどこのような新しいビューモデルを作成しました。コントローラのビューモデルに値が渡されません

public class createViewModel 
{ 
    public Change Changevm = new Change(); 

    public List<RequestType> rTypes = new List<RequestType>(); 
} 

Easy Peasy。今私のコントローラで私のPOST actionresultはこのようになります。

[HttpPost] 
    public ActionResult Create(createViewModel _newViewModel) 
    { 
     try 
     { 
      // TODO: Add insert logic here 
      if(ModelState.IsValid) 
      { 
       using (UserRequestContextDataContext db = new UserRequestContextDataContext()) 
       { 

        Request crObj = new Request(); 
        crObj.Title = _createViewModel.Changevm.Title.ToString(); 
        crObj.Description = _createViewModel.Changevm.Description.ToString(); 

        db.Requests.InsertOnSubmit(crObj); 
        db.SubmitChanges(); 
       } 
      } 

      return RedirectToAction("Create"); 
     } 
     catch(Exception ex) 
     { 

      return View("Error", new HandleErrorInfo(ex,"Change","Create")); 
     } 
    } 

したがって、実行時にフォームからビューモデルに渡されるデータがありません。私はそれがちょうど私のnoobishnessだと確信しています。ステップスルーすると、すべてのフィールドがnullになります。私は実際にビューモデルにバインドする場所を理解していません。私がこのことを完全にやっているのであれば、気軽に提案してください。

ヘルプ、または適切な資料の指示は素晴らしいです。

< <更新日: これは私の見解ですが、残念です。

@model UserRequests.ViewModels.createViewModel 

@{ 

ViewBag.Title = "Create Change"; 


List<SelectListItem> listItems = new List<SelectListItem>(); 
for (int x = 1; x < 6; x++) 
{ 
    listItems.Add(new SelectListItem 
    { 
     Text = x.ToString(), 
     Value = x.ToString() 
    }); 
} 


List<SelectListItem> listTypes = new List<SelectListItem>(); 

foreach(var item in Model.rTypes) 
{ 
    listTypes.Add(new SelectListItem 
    { 
     Text = item.Name.ToString(), 
     Value = item.Id.ToString() 
    }); 
} 

} 
<h2>Create a Request</h2> 

@using (Html.BeginForm()) 
{ 
@Html.AntiForgeryToken() 

<div class="form-horizontal"> 
    <h4>Fill out this form as completely as possible.</h4> 
    <hr /> 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
    <div class="form-group"> 
     @Html.LabelFor(model => model.Changevm.Title, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-5"> 
      @Html.EditorFor(model => model.Changevm.Title, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Changevm.Title, "", new { @class = "text-danger" }) 
     </div> 
     @Html.LabelFor(model => model.Changevm.Type, htmlAttributes: new { @class = "control-label col-md-1" }) 
     <div class="col-md-3"> 
      @Html.DropDownListFor(model => model.Changevm.Type, listTypes ,"-- Choose One -- ", new { htmlAttributes = new { @class = "" } }) 

      @Html.ValidationMessageFor(model => model.Changevm.Type, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Changevm.Description, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-5"> 
      @Html.TextAreaFor(model => model.Changevm.Description, new { @class = "form-control", @rows = 5, @id = "editor1" }) 
      @Html.ValidationMessageFor(model => model.Changevm.Description, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Changevm.SubmitDate, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-3"> 
      @Html.EditorFor(model => model.Changevm.SubmitDate, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Changevm.SubmitDate, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Changevm.BusinessUnit, htmlAttributes: new { @class = "control-label col-md-2", }) 
     <div class="col-md-5"> 
      @Html.EditorFor(model => model.Changevm.BusinessUnit, new { htmlAttributes = new { @class = "form-control", @placeholder = ModelMetadata.FromLambdaExpression(x => x.Changevm.BusinessUnit, ViewData).Watermark } }) 
      @Html.ValidationMessageFor(model => model.Changevm.BusinessUnit, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Changevm.ModuleName, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-5"> 
      @Html.EditorFor(model => model.Changevm.ModuleName, new { htmlAttributes = new { @class = "form-control", @placeholder = ModelMetadata.FromLambdaExpression(x => x.Changevm.ModuleName, ViewData).Watermark, @style = "color:black"} }) 
      @Html.ValidationMessageFor(model => model.Changevm.ModuleName, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Changevm.UrgencyNum, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-3"> 
      @Html.DropDownListFor(model => model.Changevm.UrgencyNum, listItems, "-- Urgency Level (1 = Least Urgent) -- ", new { htmlAttributes = new { @class = "" } }) 
      @Html.ValidationMessageFor(model => model.Changevm.UrgencyNum, "", new { @class = "text-danger" }) 
     </div> 

    </div> 

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-5"> 
      <button type="reset" class="btn btn-default">Cancel</button> 
      <input type="submit" value="Create" class="btn btn-success" /> 
     </div> 
    </div> 
</div> 
} 

<div> 
@Html.ActionLink("Back to Home", "Index", "Home") 
</div> 
+0

「View」の外観はどうですか? '.cshtml'部分です。 –

+1

ビューでモデルをバインドする必要があります。そこで何が起こっているのかを見るには、ビューコードを確認する必要があります。 – HarvP

+0

あなたのビューでモデルを指定していますか? '@model Test.Models.createViewModel'として –

答えて

0

これは問題ですが、私はviewmodelの性質がそのように扱わ見たことがない場合は必ず、ゲッターとセッターを使用するのviewmodelのプロパティを変更しないようにしてください。

public class createViewModel 
{ 
    public Change Changevm { get; set; } 

    public List<RequestType> rTypes { get; set; } 
} 

コメントに記載されているように、そこにもいくつか起こっている可能性があるので、あなたの意見を投稿するべきです。

+0

看護師の間違い。これはうまくいった。 –

+0

悲しいことに私はそれがなぜそれを修正するのかわからない、その魔法! –

関連する問題