我试图在另一个视图中将我的部分视图注入我的数据库值
这是我的模特;
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);
}
现在这里是我使用businees模型的部分视图代码
@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&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---->
联系人视图使用联系人模型,Getallinfo视图使用businessmodel 问题是局部视图显示但没有数据附加到地址:,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”)
答案 0 :(得分:3)
您可以将业务表单模型添加为联系表单模型的属性,然后从传递正确模型的联系人部分调用业务模型部分。
例如。
@ Html.Partial(“myBusinessPartial”,model.BusinessModel)