有没有办法让页面的一部分呈现像一个小的子页面,如组件?
例如,如果我的所有页面上都有购物车?
在ASP.Net MVC中,我想根据renderview查询字符串参数呈现不同的局部视图.
因此,为用户提供选择按缩略图或详细信息查看产品的工具.
我可以访问控制器中的所选参数,但我不知道如何或者,如果我应该将它与产品列表一起传递给视图,那么视图可以实现逻辑来决定显示哪个部分视图?
public ActionResult Products(string id, int? renderview)
{
var products = productRepository.GetProducts(id).ToList();
return View("Products", products);
}
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MLBWebRole.Models.Product>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Products
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Products</h2>
<p>This is the Products page</p>
<p><a href="?renderview=0">thumbnails</a> <a href="?renderview=1">details</a></p>
<% if (renderview == 1)
{%>
<% Html.RenderPartial("ProductsDetailList"); %>
<% }
else
{ %>
<% Html.RenderPartial("ProductsThumbnailList"); %>
<% } %>
</asp:Content>
Run Code Online (Sandbox Code Playgroud) 这可能是一个新手问题.
当我创建ASP.NET MVC2应用程序时,会创建一个带有Action LogIn的帐户控制器,如下所示:
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (MembershipService.ValidateUser(model.UserName, model.Password))
{
FormsService.SignIn(model.UserName, model.RememberMe);
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
现在,我不希望有一个登录页面,我希望将登录控件作为更大页面的一部分.所以,我将Login.aspx更改为Login.ascx,并将其集成到我的主视图中,使用Html.RenderPartial或Html.RenderAction.
如果登录成功,两者都像魅力.如果不是,那就是
return View(model)
Run Code Online (Sandbox Code Playgroud)
杀了我 我想要的是回到我的主页面(称之为Home/Index),但是带有局部视图的错误信息.
return RedirectToAction("Index", "Home")
Run Code Online (Sandbox Code Playgroud)
显然不起作用.
提示?
我想创建一个生成HTML电子邮件的自定义类.我希望电子邮件的内容来自"电子邮件视图脚本"目录.所以概念是我可以像创建普通视图脚本(能够指定类变量等)一样创建HTML电子邮件视图脚本,并且视图脚本将呈现为电子邮件的HTML主体.
例如,在控制器中:
$email = My_Email::specialWelcomeMessage($toEmail, $firstName, $lastName);
$email->send();
Run Code Online (Sandbox Code Playgroud)
该My_Email::specialWelcomeMessage()函数将执行以下操作:
public static function specialWelcomeMessage($toEmail, $firstName, $lastName) {
$mail = new Zend_Mail();
$mail->setTo($toEmail);
$mail->setFrom($this->defaultFrom);
$mail->setTextBody($this->view->renderPartial('special-welcome-message.text.phtml', array('firstName'=>$firstName, 'lastName'=>$lastName));
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,如果我能找到一种方法使specialWelcomeMessage()函数像这样简单,那将是最好的:
public static function specialWelcomeMessage($toEmail, $firstName, $lastName) {
$this->firstName = $firstName;
$this->lastName = $lastName;
//the text body and HTML body would be rendered automatically by being named $functionName.text.phtml and $functionName.html.phtml just like how controller actions/views happen
}
Run Code Online (Sandbox Code Playgroud)
然后,它将呈现特殊的welcome-message.text.phtml和special-welcome-message.html.phtml脚本:
<p>Thank you <?php echo $this->firstName; ?> <?php echo $this->lastName; ?>.</p>
Run Code Online (Sandbox Code Playgroud)
如何从视图脚本或控制器外部调用部分视图助手?我是以正确的方式接近这个吗?或者这个问题有更好的解决方案吗?
我正在寻找其他人通常如何组织他们的部分多态资源.
例:
我有多Image态,取决于什么imageable,我想显示略有不同的东西.
我有一个部分images/_image,可以打电话render imageable.images.我目前的心态是让我的图像部分检查是什么类型imageable,然后有另一部分,特定于该情况.我的组织将是这样的:
images/
_image.html.haml
_product.html.haml
_post.html.haml
_user.html.haml
Run Code Online (Sandbox Code Playgroud)
我的_image部分看起来像:
%div
= render :partial => "images/#{imageable.type}"
Run Code Online (Sandbox Code Playgroud)
这看起来像是一种糟糕的方法,还是采取了错误的做法?我认为render imageable.images从任何地方打电话比在render :partial => ...全场打电话要好得多.
任何想法将不胜感激.你是怎么做到的?
编辑:很长一段时间过去了,我仍然想知道是否有人对此有任何意见.抛出赏金,看看是否引起了一些注意.
我在局部视图中定义了一个部分,我想从视图中指定部分的内容.但我无法想办法.在asp.net用户控件中,我们可以定义asp:占位符,并指定用户控件所在的aspx中的内容.我会很高兴任何建议.
谢谢
[edit]这是asp.net用户控件,我想将其转换为razor局部视图
用户控制:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SpryListView.ascx.cs" Inherits="SpryListView" %>
<div spry:region="<%=this.SpryDataSetName%>" id="region<%=this.ID%>" style="overflow:auto;<%=this.DivStyle%>" >
<table class="searchList" cellspacing="0" style="text-align:left" width="100%">
<thead>
<tr>
<asp:PlaceHolder ID="HeaderColumns" runat="server"></asp:PlaceHolder>
</tr>
</thead>
</table>
Run Code Online (Sandbox Code Playgroud)
用户控制代码:
public partial class SpryListView : System.Web.UI.UserControl
{
private string spryDataSetName ;
private string noDataMessage = "Arad???n?z kriterlere uygun kay?t bulunamad?.";
private bool callCreatePaging;
private string divStyle;
private ITemplate headers = null;
private ITemplate body = null;
[TemplateContainer(typeof(GenericContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate HeaderTemplate
{
get
{
return headers;
}
set
{
headers …Run Code Online (Sandbox Code Playgroud) 我正在一个解决方案下构建几个不同的asp.net mvc 3 Web项目.我希望能够在所有这些项目中使用一些视图,因为它们将在2或3个不同的页面上显示相同的内容(根据应用程序插入一些自定义内容).
这可能吗?
我在使用不显眼的ajax从联系我们表单发布数据时遇到了麻烦.
我的部分视图是这样的:
@model site.ViewModel.Home.ContactUsViewModel
@using (Ajax.BeginForm("_ContactUsPartial", "Home",
new AjaxOptions { UpdateTargetId = "result" }))
{
<fieldset>
<legend>Contact Us</legend>
@Html.ValidationSummary(true)
<div id="result"></div>
<div class="editor-label">
@Html.LabelFor(m => m.FullName)
@Html.ValidationMessageFor(m => m.FullName)</div>
<div class="editor-field">@Html.TextBoxFor(m => m.FullName)</div>
<!-- other fields from model -->
<input type="submit" value="Submit"/>
</fieldset>
}
Run Code Online (Sandbox Code Playgroud)
我的部分视图放在另一个页面上,如下所示:
@Html.Partial("_ContactUsPartial", new ContactUsViewModel());
Run Code Online (Sandbox Code Playgroud)
我的家庭控制器是这样的:
[HttpPost]
public ActionResult _ContactUsPartial(ContactUsViewModel viewModel)
{
if (ModelState.IsValid)
{
try
{
//send email
return Content("Thanks for your message!");
}
catch (Exception ex)
{
return Content("Problem sending the email.");
}
}
return …Run Code Online (Sandbox Code Playgroud) 如何在局部视图中将所有本地作为哈希.
# mainview.html.haml:
= render 'v1', p1: 100, p2: 'some_value', _other_values_
Run Code Online (Sandbox Code Playgroud)
在局部视图中:
# _v1.html.haml
# do something here...
# render another partial view
= render 'another_view', locals: locals # what to write here???
Run Code Online (Sandbox Code Playgroud)
如何访问本地中的所有变量作为哈希.我不想在本地列出所有变量.
我有一个工作搜索功能,但希望在提交时包含jquery进度条.有时,部分视图最多可能需要5秒才能加载.需要进度条,因此用户不会按下提交/输入键.我想隐藏进度条直到提交被按下.
有没有办法让百分比实际测量加载时间?
Jquery进度条:https: //jqueryui.com/progressbar/#label
视图:
@model Application.Web.ViewModels.BillingViewModel
@{
ViewBag.Title = "BillingLetterSearch";
Layout = "~/Views/Shared/_LayoutFullWidth.cshtml";
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
.ui-progressbar {
position: relative;
}
.progress-label {
position: absolute;
left: 50%;
top: 4px;
font-weight: bold;
text-shadow: 1px 1px 0 #fff;
}
</style>
<div class="row">
<panel>
<table class="table">
<thead>
<tr>
<th>Search By Employee Number:</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="3">
@Html.TextBoxFor(x => Model.EmployeeNumber, new { @class = "form-control", @id = "EmployeeNumber", @autofocus = …Run Code Online (Sandbox Code Playgroud) partial-views ×10
asp.net-mvc ×5
ajaxform ×1
asp.net ×1
email ×1
html-email ×1
jquery ×1
organization ×1
php ×1
progress-bar ×1
razor ×1
sections ×1
view ×1
views ×1