Reputation: 228
\hi guys. i have a class as you can see here :
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; }
}
I have a controller called index
.i have to save just one record in my database .if the record is exist in database i should show it on the page to the user so i used this :
public ActionResult Index()
{
if (_configurationRepository.GetConfiguration().Count() > 0)
return View(_configurationRepository.GetConfiguration().First());
return View();
}
as you can see if the record is exist i pass it to the view but the problem is my view can't show the data.the data should be bind to the text boxes.my view is like this :
@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>
}
Upvotes: 0
Views: 88
Reputation: 56688
Just the fact that the name of the input field is the same as the name of the model property is not sufficient to bind the latter to the former. You need to actually insert the value. Example:
<input class="form-control" id="Title" name="Title" type="text" value="@Model.Title">
Better yet, use special methods of HtmlHelper. Example:
Html.TextBoxFor(m => m.Title)
Upvotes: 1