2017-05-25 9 views
1

私は最近、ASP.NET MVC5を学習しています。同じビューでテーブルとフォームの両方を表示する方法

フォームと表の両方を表示しようとしています(部分ビューとして返されます)。しかし、このエラーが発生しています。ここで

System.NullReferenceException: Object reference does not set to an instance of an object. 

は私のモデルである:私は私の部分図を入れたい

public class Prescription 
{ 
    [Key] 
    public int PrescriptionID { get; set; } 

    [ForeignKey("Assessment")] 
    public int? AssessmentID { get; set; } 
    public Assessment Assessment { get; set; } 

    [ForeignKey("Medicine")] 
    [Display(Name ="Prescription")] 
    public int? MedcineID { get; set; } 
    public Medicine Medicine { get; set; } 
} 

私のメインビュー:

@using ClinicManagemet 
@model ClinicManagemet.Models.Prescription 


@{ 
ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 

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

<div class="form-horizontal"> 
    <h4>Prescription</h4> 
    <hr /> 


    <div class="form-group"> 

     @Html.LabelFor(model => model.MedcineID, "MedcineID", htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.DropDownList("MedcineID", null, htmlAttributes: new { @class = "form-control" }) 

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

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" value="Create" class="btn btn-default" /> 
     </div> 
    </div> 
</div> 
} 

    @Html.Action("ViewPrescription","Assessments") 

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

マイ部分図:

@model IEnumerable<ClinicManagemet.Models.Prescription> 

<table class="table"> 
<tr> 
    <th> 
     @Html.DisplayNameFor(model => model.Assessment.Complaint) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.Medicine.MedicineName) 
    </th> 
    <th></th> 
</tr> 

@foreach (var item in Model) { //Here is the line where I get the error 
<tr> 
    <td> 
     @Html.DisplayFor(modelItem => item.Assessment.Complaint) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.Medicine.MedicineName) 
    </td> 
    <td> 
     @Html.ActionLink("Edit", "Edit", new { id=item.PrescriptionID }) | 
     @Html.ActionLink("Details", "Details", new { id=item.PrescriptionID }) | 
     @Html.ActionLink("Delete", "Delete", new { id=item.PrescriptionID }) 
    </td> 
</tr> 
} 

</table> 

マイ部分ビューのコントローラ:

public ActionResult ViewPrescription() 
    { 
     return PartialView(); 
    } 

編集:これを修正すれば、何かを挿入するたびに部分的な表示が更新されるので、Ajaxを追加しようとします。

+0

私の答えはベローズ –

答えて

1

{ 
//Fetch the data here 
return PartialView(model); 
} 

はそれが役に立てば幸い、データを返す、

@{ 
    Html.RenderAction("ViewPrescription","YourControllerName") 
} 

そして、あなたのViewPrescription方法でこのようなあなたの部分的なビューをロードします。

+1

を確認してくださいそれは動作します!どうもありがとうございます。 – kielou

+1

それはうまくいってうれしい:)答えとしてマークしてください。 –

+0

ありがとう、今私はajaxを適用することができます – kielou

0

ビューを返すときにモデルを部分ビューに渡すことはありません。

public ActionResult ViewPrescription() 
    { 
     ClinicManagemet.Models.Prescription model = _service.GetPerscription(); 

     return PartialView(model); 
    } 
関連する問題