简体   繁体   English

从控制器传递数据以在asp.net中查看

[英]Pass data from controller to view in asp.net

\\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 : 我有一个名为index的控制器。我只需要在数据库中保存一条记录。如果该记录存​​在于数据库中,则应在页面上向用户显示该记录,因此我使用了以下方法:

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>
}

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. 输入字段的名称与model属性的名称相同的事实不足以将后者绑定到前者。 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. 更好的是,使用HtmlHelper的特殊方法。 Example: 例:

Html.TextBoxFor(m => m.Title)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM