簡體   English   中英

如何在MVC中使用表數據填充PartialView

[英]how to populate partialview with table data in mvc

我正在嘗試將我的partialview與我的數據庫值注入另一個視圖中

這是我的模特

public class BusinessModel
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int id { get; set; }

    [Display(Name = "Address :")]
    public string Address { get; set; }

    [Required(ErrorMessage = "Enter your Email Address")]
    [Display(Name = "Email :")]
    [RegularExpression(@"^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$", ErrorMessage = "Email is not in proper format")]
    public string Email { get; set; }
    [Required]
    [Display(Name = "ContactNo :")]
    [StringLength(10, MinimumLength = 10)]
    [DataType(DataType.PhoneNumber)]
    public string ContactNo { get; set; }
}

}

這是我的businesslogic類方法來返回信息

    public BusinessModel GetDetailsById(int id)
    {
        using (var busrep = new BusinessRepository())
        {
            Business b = busrep.GetById(id);
            var bview = new BusinessModel();
            if (b != null)
            {
                bview.Address = b.Address;
                bview.Email = b.Email;
                bview.ContactNo = b.ContactNo;
            }
            return bview;
        }
    }

這是我的控制器動作

 [HttpGet]
    public ActionResult GetAllInfo(int id)
    {
        BusinessModel emp = _logic.GetDetailsById(id);
        return View(emp);
    }

現在這是我使用業務模型的局部視圖代碼

@model Template.Model.BusinessModel
@using Template.Model
<div>

<hr />
<dl class="dl-horizontal">
    @Html.HiddenFor(model => model.id)
    <dt>
        @Html.DisplayNameFor(model => model.Address)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Address)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.Email)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Email)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.ContactNo)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.ContactNo)
    </dd>

</dl>

現在這是我的“聯系人”視圖,我希望我的“部分視圖”顯示業務詳細信息

@using Template.Model
@model Template.Model.ContactModel
@{
ViewBag.Title = "Contact";
Layout = "~/Views/shared/_BootstrapLayout.basic.cshtml";
}
<!DOCTYPE CSHTML>
<html>
<head>
<title>Naturex Website Template | Contact :: W3layouts</title>
<link href="/Content/style.css" rel="stylesheet" type="text/css" media="all" />
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="/Content/superfish.css" media="screen">
<script src="/Scripts/jquery.js"></script>
<script src="/Scripts/superfish.js"></script>
<script type="text/javascript" src="/Scripts/move-top.js"></script>
<script type="text/javascript" src="/Scripts/easing.js"></script>
<script>
    (function($){ //create closure so we can safely use $ as alias for jQuery
        $(document).ready(function(){
            // initialise plugin
            var example = $('#example').superfish({
                //add options here if required
            });
            // buttons to demonstrate Superfish's public methods
            $('.destroy').on('click', function(){
                example.superfish('destroy');
            });
            $('.init').on('click', function(){
                example.superfish();
            });
            $('.open').on('click', function(){
                example.children('li:first').superfish('show');
            });
            $('.close').on('click', function(){
                example.children('li:first').superfish('hide');
            });
        });
    })(jQuery);
</script>
</head>
<body>
<!---start-wrap---->
<div class="wrap">
    <!---start-header---->
    <div class="header">
        <div class="logo">
            <a href="/Views/Home/Index.cshtml"><img src="~/Images/PhysiqueAidLogo.png" title="Physique-Aid" style="height : 90px;width : 153px;" /> </a>
        </div>
        <div class="top-social-icons">
            <ul>
                <li><a class="icon1" href="#"> </a></li>
                <li><a class="icon2" href="#"> </a></li>
                @*<li><a class="icon3" href="#"> </a></li>*@
                <li><a class="icon4" href="#"> </a></li>
            </ul>
        </div>
        <div class="clear"> </div>
    </div>
    <div class="clear"> </div>
    <!---End-header---->
    <!---start-top-nav---->
    @*@<div class="top-nav">
        <ul class="sf-menu sf-menu2" id="example">
            <li class="current home">
                @Html.ActionLink(" ", "Index", "Home")

            </li>
            <li class="active">
                @Html.ActionLink("About", "About", "Home")

            </li>

            <li>
                @Html.ActionLink("Contact", "Contact", "Home")


            </li>
            <li>
                @Html.ActionLink("Booking", "MakeBooking", "Booking")

                @*<a href="/Views/Home/Contact.cshtml">Contact</a>*@
            @*</li>
            <li>
                @Html.ActionLink("Gallery", "gallery", "Home")
            </li>

            <li class="current">
                <a href="#">Register and Log In</a>
                <ul>
                    <li @Html.Partial("_LoginPartial")></li>*@
                    @*<li @Html.ActionLink("Login", "Index", "Login")></li>
                    <li @Html.ActionLink("Register", "Index", "Register")></li>*@
                @*</ul>
            </li>

            <div class="clear"> </div>
        </ul>
    </div>
    <div class="clear"> </div>


</div>*@
<!---End-header---->

    </div>

    <!---start-content----->
    <div class="content">

        <!---start-Contact----->
        <div class="contact">
            <div class="wrap">
                <h1>Contact<span> Us</span></h1>
                <div class="section group">
                    <div class="col span_1_of_3">
                        <div class="contact_info">
                            <h3>Find Us Where You Are</h3>
                            <div class="map">
                                <p>Tell us where to go and we will find you.</p>
                                @*<iframe width="100%" height="175" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.co.in/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=Lighthouse+Point,+FL,+United+States&amp;aq=4&amp;oq=light&amp;sll=26.275636,-80.087265&amp;sspn=0.04941,0.104628&amp;ie=UTF8&amp;hq=&amp;hnear=Lighthouse+Point,+Broward,+Florida,+United+States&amp;t=m&amp;z=14&amp;ll=26.275636,-80.087265&amp;output=embed"></iframe><br><small><a href="https://maps.google.co.in/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q=Lighthouse+Point,+FL,+United+States&amp;aq=4&amp;oq=light&amp;sll=26.275636,-80.087265&amp;sspn=0.04941,0.104628&amp;ie=UTF8&amp;hq=&amp;hnear=Lighthouse+Point,+Broward,+Florida,+United+States&amp;t=m&amp;z=14&amp;ll=26.275636,-80.087265" style="color:#666;text-align:left;font-size:12px">View Larger Map</a></small>*@
                            </div>
                        </div>
                        <div class="company_address">
                            <h3>Contact Physique-Aid:</h3>
                           ***@Html.Partial("GetAllinfo")***
                            <p>Follow on: <span>Facebook</span>, <span>Twitter</span> <span>Instagram</span></p>
                        </div>
                    </div>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="col span_2_of_3">
    <div class="contact-form">
        <h3>Contact Us</h3>

        <div class="form-horizontal">
            <div>
                <span><label>FIRSTNAME</label></span>
                <span><input name="FirstName" type="text" class="textbox"></span>
                @Html.ValidationMessageFor(model => model.FirstName, "", new {@class = "text-danger"})

            </div>
            <div>
                <span><label>LAST NAME</label></span>
                <span><input name="LastName" type="text" class="textbox"></span>
                @Html.ValidationMessageFor(model => model.LastName, "", new {@class = "text-danger"})
            </div>
            <div>
                <span><label>Email</label></span>
                <span><input name="Email" type="text" class="textbox"></span>
                @Html.ValidationMessageFor(model => model.Email, "", new {@class = "text-danger"})
            </div>
            <div>
                <span><label>ENQUIRIES</label></span>
                <span><textarea name="Contacts"> </textarea></span>
                @Html.ValidationMessageFor(model => model.Contacts, "", new {@class = "text-danger"})
            </div>
            <div>
                <span><input type="submit" value="Submit"></span>
            </div>

        </div>
    </div>
</div>
}
                    @if (User.IsInRole("Admin"))
                    {
                        <div>
                            @Html.ActionLink("EditDetails", "")
                        </div>
                    }
            </div>
            <!---End-contact----->
        </div>
        <!---End-content----->
    </div>
    <div class="clear"></div>
    <div class="clear"></div>
    <!---start-footer----->
    <div class="footer">
        <div class="wrap">
            <div class="footer-left">
                <a href="Index.cshtml">Physique-Aid</a>
            </div>
            <div class="footer-right">

                <script type="text/javascript">
                    $(document).ready(function () {

                        var defaults = {
                            containerID: 'toTop', // fading element id
                            containerHoverID: 'toTopHover', // fading element hover id
                            scrollSpeed: 1200,
                            easingType: 'linear'
                        };


                        $().UItoTop({ easingType: 'easeOutQuart' });

                    });
                </script>
                <a href="#" id="toTop" style="display: block;"><span id="toTopHover" style="opacity: 1;"></span></a>
            </div>
            <div class="clear"> </div>
        </div>
    </div>
    <!---End-footer----->
    <!---End-wrap---->

聯系人視圖使用聯系人模型,而Getallinfo視圖使用業務模型問題是顯示了部分視圖,但是沒有附加到以下地址的數據:,Email:,ContactNo :,這些都存在於數據庫中

需要援助

here are the two models

我要顯示詳細信息的業務

public class BusinessModel
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int id { get; set; }

    [Display(Name = "Address :")]
    public string Address { get; set; }

    [Required(ErrorMessage = "Enter your Email Address")]
    [Display(Name = "Email :")]
    [RegularExpression(@"^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$", ErrorMessage = "Email is not in proper format")]
    public string Email { get; set; }
    [Required]
    [Display(Name = "ContactNo :")]
    [StringLength(10, MinimumLength = 10)]
    [DataType(DataType.PhoneNumber)]
    public string ContactNo { get; set; }
}

}

和聯系人主視圖的模型

public class ContactModel
{
    [Key]
    [Required(ErrorMessage = "Field Cannot be left empty. Please enter First Name")]
    [RegularExpression("^([a-zA-Z0-9 .&'-]+)$", ErrorMessage = "Name can NOT contain special chracters and/or numbers. Please Re-Enter")]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Field Cannot be left empty. Please enter Last Name")]
    [RegularExpression("^([a-zA-Z0-9 .&'-]+)$", ErrorMessage = "Surname can NOT contain special chracters and/or numbers. Please Re-Enter")]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    [Required(ErrorMessage = "Field Cannot be left empty. Please enter Email Address")]
    [EmailAddress(ErrorMessage = "Please enter email address in correct format, Eg; something@example.com")]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required(ErrorMessage = "Field Cannot be left empty")]
    [Display(Name = "Contacts")]
    public string Contacts { get; set; }


}

聯系人視圖html是上面的主視圖。

這是我用來顯示部分視圖的代碼。 @ Html.Partial(“ GetAllinfo”)

您可以將業務表單模型添加為聯系人表單模型的屬性,然后從傳入正確模型的聯系人部分中調用業務模型部分。

例如。

@ Html.Partial(“ myBusinessPartial”,model.BusinessModel)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM