[英]how to populate partialview with table data in mvc
Im trying to inject my partialview with my database values within another view 我正在尝试将我的partialview与我的数据库值注入另一个视图中
here is my model; 这是我的模特
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; }
}
} }
here is my businesslogic class method to return the information 这是我的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;
}
}
here is my controller action 这是我的控制器动作
[HttpGet]
public ActionResult GetAllInfo(int id)
{
BusinessModel emp = _logic.GetDetailsById(id);
return View(emp);
}
now here is my partial view code using the businees model 现在这是我使用业务模型的局部视图代码
@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>
Now here is my Contact view where i want my partialview to display business details 现在这是我的“联系人”视图,我希望我的“部分视图”显示业务详细信息
@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&source=s_q&hl=en&geocode=&q=Lighthouse+Point,+FL,+United+States&aq=4&oq=light&sll=26.275636,-80.087265&sspn=0.04941,0.104628&ie=UTF8&hq=&hnear=Lighthouse+Point,+Broward,+Florida,+United+States&t=m&z=14&ll=26.275636,-80.087265&output=embed"></iframe><br><small><a href="https://maps.google.co.in/maps?f=q&source=embed&hl=en&geocode=&q=Lighthouse+Point,+FL,+United+States&aq=4&oq=light&sll=26.275636,-80.087265&sspn=0.04941,0.104628&ie=UTF8&hq=&hnear=Lighthouse+Point,+Broward,+Florida,+United+States&t=m&z=14&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---->
the contact view uses contact model and the Getallinfo view uses businessmodel problem is that the partial view displays but has no data attached to the address:,Email:,ContactNo: which all exist in the database 联系人视图使用联系人模型,而Getallinfo视图使用业务模型问题是显示了部分视图,但是没有附加到以下地址的数据:,Email:,ContactNo :,这些都存在于数据库中
In need of sum assistance 需要援助
here are the two models
business that i want to display details 我要显示详细信息的业务
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; }
}
} }
and the model for the Contact main view 和联系人主视图的模型
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; }
}
the contact view html is the one already above which is the main view. 联系人视图html是上面的主视图。
this is the code i use to display the partial view. 这是我用来显示部分视图的代码。 @Html.Partial("GetAllinfo")
@ Html.Partial(“ GetAllinfo”)
You could add Business form Model as a property against the contact form model and then call the business model partial from the contact partial passing in the correct model. 您可以将业务表单模型添加为联系人表单模型的属性,然后从传入正确模型的联系人部分中调用业务模型部分。
for example. 例如。
@Html.Partial("myBusinessPartial", model.BusinessModel) @ Html.Partial(“ myBusinessPartial”,model.BusinessModel)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.