2015-10-27 37 views
0

\ hi guys。 我有一個類,你可以在這裏看到:從控制器傳遞數據到asp.net中查看

public class Configuration 
    { 
     public int Id { get; set; } 
     public string Title { get; set; } 
     public string Telephone { get; set; } 
     public string Fax { get; set; } 
     public string Email { get; set; } 
     public string ZipCode { get; set; } 
     public string WebSite { get; set; } 
     public string Address { get; set; } 
     public string GPS { get; set; } 

     [System.ComponentModel.DataAnnotations.Schema.NotMapped] 
     public HttpPostedFileBase ImgByte { get; set; } 
     public string LogoUrl { get; set; } 
    } 

我有一個名爲index。我已經保存一條記錄在我的數據庫。如果記錄控制器在數據庫中我應該顯示在頁面上給用戶,所以我用這個:

public ActionResult Index() 
     { 
      if (_configurationRepository.GetConfiguration().Count() > 0) 
       return View(_configurationRepository.GetConfiguration().First()); 
      return View(); 
     } 

,你可以看到,如果該記錄是存在我把它傳遞給看法,但問題是我的看法不能顯示應該綁定到的數據。數據text boxes.my view is like:

@model CMS.DomainClass.Configuration 
@{ 
    ViewBag.Title = "تنظیمات سایت"; 
    Layout = "~/Areas/FAdmin/Views/Shared/_FAdminMaster.cshtml"; 
} 

<!-- Content Header (Page header) --> 
<section class="content-header"> 
    <h1>تنظیمات 

    </h1> 

</section> 


@using (@Html.BeginForm("Create", "Configuration", FormMethod.Post, 
    new { id = "form", enctype = "multipart/form-data" })) 
{ 

    if (TempData["Error"] != null) 
    { 
    <div class="pad margin no-print"> 
     <div class="callout callout-info" style="margin-bottom: 0!important;background-color: #ea0000 !important;"> 
      <h4>پیام:</h4> 
      @TempData["Error"] 
     </div> 
    </div> 
    } 
    if (TempData["Information"] != null) 
    { 
    <div class="pad margin no-print"> 
     <div class="callout callout-info" style="margin-bottom: 0!important;background-color: orangered !important"> 
      <h4>پیام:</h4> 
      @TempData["Information"] 
     </div> 
    </div> 
    } 
    if (TempData["Success"] != null) 
    { 
    <div class="pad margin no-print"> 
     <div class="callout callout-info" style="margin-bottom: 0!important;background-color: #00A65A !important"> 
      <h4>پیام:</h4> 
      @TempData["Success"] 
     </div> 
    </div> 
    } 
    <div class="row"> 
     <!-- left column --> 
     <div class="col-md-6"> 
      <!-- general form elements --> 
      <div class="box box-primary"> 
       <div class="box-header with-border"> 
        <h3 class="box-title">اطلاعات پایه</h3> 
       </div> 
       <!-- /.box-header --> 
       <!-- form start --> 
       <div class="box-body"> 
        <div class="form-group"> 
         <label for="Title">عنوان سایت</label> 
         <input class="form-control" id="Title" name="Title" type="text"> 
        </div> 
        <div class="form-group"> 
         <label for="WebSite">وب سایت</label> 
         <input class="form-control" id="WebSite" name="WebSite" type="tel"> 
        </div> 
        <div class="form-group"> 
         <label for="Email">ایمیل</label> 
         <input class="form-control" id="Email" name="Email" type="tel"> 
        </div> 
        <div class="form-group"> 
         <label for="ImgByte">لوگو</label> 
         <input id="ImgByte" name="ImgByte" type="file"> 
         <p class="help-block">سایز : 33*114</p> 
        </div> 
       </div> 
       <!-- /.box-body --> 

      </div> 
      <!-- /.box --> 

     </div> 
     <!--/.col (left) --> 
     <!-- right column --> 
     <div class="col-md-6"> 
      <!-- Horizontal Form --> 
      <div class="box box-primary"> 
       <div class="box-header with-border"> 
        <h3 class="box-title">اطلاعات پایه</h3> 
       </div> 
       <!-- /.box-header --> 
       <!-- form start --> 
       <div class="box-body"> 
        <div class="form-group"> 
         <label for="Telephone">تلفن</label> 
         <input class="form-control" id="Telephone" name="Telephone" type="tel"> 
        </div> 
        <div class="form-group"> 
         <label for="Fax">فکس</label> 

         <input class="form-control" id="Fax" name="Fax" type="tel"> 
        </div> 
        <div class="form-group"> 
         <label for="ZipCode">کد پستی</label> 

         <input class="form-control" id="ZipCode" name="ZipCode" type="number"> 
        </div> 
        <div class="form-group"> 
         <label for="ZipCode">آدرس</label> 

         <textarea class="form-control" rows="3" id="Address" name="Address"></textarea> 

        </div> 
        <div class="form-group"> 
         <label for="GPS">GPS</label> 

         <input class="form-control" id="GPS" name="GPS" type="text"> 
        </div> 
       </div> 
       <!-- /.box-body --> 
      </div> 
      <!-- /.box --> 

     </div> 
     <!--/.col (right) --> 
    </div> 
    <div class="row"> 
     <div class="box-footer" style="direction: ltr"> 
      <button type="submit" class="btn btn-success">ذخیره</button> 
      <button type="submit" class="btn btn-gray">انصراف</button> 

     </div> 
    </div> 
} 
+0

您需要轉到MVC站點並完成教程,特別是如何使用html助手創建視圖,以便綁定到您的模型屬性 –

回答

1

只是輸入字段的名稱與模型屬性的名稱相同的事實不足以將後者綁定到前者。您需要實際插入值。例如:

<input class="form-control" id="Title" name="Title" type="text" value="@Model.Title"> 

更好的是,使用HtmlHelper的特殊方法。示例:

Html.TextBoxFor(m => m.Title) 
相關問題