How to show your hidden partial view programmatically in MVC - asp.net-mvc

I have a partial view _ABC.cshtml that is hidden initially.
<div id="overview" style="display:none">
<h1> Overview</h1>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 leftlist">
</div>
<div class="col-xs-9 col-sm-9 col-md-9 col-lg-9 overview">
<h3 class="isppagetitle">#Resources.Overview</h3>
<div class="row col-xs-12 col-sm-12 col-md-12 col-lg-12 content">
<div class="row">
......
But now from other view I want it to be rendered .
public ActionResult Index()
{
return PartialView("_AMgmtPartial");
}
I did in _AMgmtPartial..
<div class="account-root">
#{
Html.RenderPartial("_CustList");
Html.RenderPartial("_ABC", new { style = "display:block" });
}
<input type="hidden" id="accountmgmturl" value='#Url.Action("", "AccountMgmt")' />
...............
...............
But this style="display:block;" did not work. What did I do wrong ?

Html.RenderPartial doesn't have a parameter that accepts HtmlAttributes. This is confirmed by looking at the documentation for it.
The parameter that you have populated would be accessible from within the Controller though:
public ActionResult Index(string style)
{
// style will equal "display:block"
return PartialView("_AMgmtPartial");
}
You could take this value and pass it to your partial view via the view bag or a view model.
public ActionResult Index(string style = "display: none")
{
// style will equal "display:block"
ViewBag.PartialStyle = style;
return PartialView("_AMgmtPartial");
}
Then this makes it accessible from within your view:
<div id="overview" style="#ViewBag.PartialStyle">
<h1> Overview</h1>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 leftlist">
</div>
<div class="col-xs-9 col-sm-9 col-md-9 col-lg-9 overview">
<h3 class="isppagetitle">#Resources.Overview</h3>
<div class="row col-xs-12 col-sm-12 col-md-12 col-lg-12 content">
<div class="row">
......
To do this though would be a dirty hack. As #Stephen mentioned a more preferable solution would be to wrap your partial view render statement in a <div> and on that you should set your initial visibility.
<div style="display: block">
#Html.Partial("_ABC");
</div>

Related

How to add div tag after fixed number of iteration in mvc

I am using Umbraco (mvc) with bootstrap.My template use partial view to load name and job title of staff in template. How I can add div (new row) after 4 iteration.
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
var selection = Model.Content.Site().FirstChild("aboutUs").Children("staff")
.Where(x => x.IsVisible());
}
#foreach(var item in selection){
<div class="row team">
<div class="col-sm-3">
<div class="thumbnail-style">
<h3><a>#item.GetPropertyValue("fullName")</a> <small>#item.GetPropertyValue("jobTitle")</small></h3>
</div>
</div>
</div>
}
Difficulty:
I want to add
<div class="row team">
</div>
after every 4th
<div class="col-sm-3">
</div>
A more easier solution would be the use of the Umbraco helper method InGroupsOf(columns).
#foreach(var group in selection.InGroupsOf(4))
{
<div class="row team">
#foreach(var item in group)
{
<div class="col-sm-3">
<div class="thumbnail-style">
<h3><a>#item.GetPropertyValue("fullName")</a> <small>#item.GetPropertyValue("jobTitle")</small></h3>
</div>
</div>
}
</div>
}
Try:
#foreach(var obj in selection.Select((item, index) => new {item, index}))
{
if(obj.index==0||obj.index%4==0){
#Html.Raw("<div class='row team'>")
}
<div class="col-sm-3">
<div class="thumbnail-style">
<h3><a>#obj.item.GetPropertyValue("fullName")</a> <small>
#obj.item.GetPropertyValue("jobTitle")</small></h3>
</div>
</div>
#if((obj.index+1)%4==0||obj.index+1==selection.Count()){
#Html.Raw("</div>")
}
}
- It will works, Use for loop instead of foreach loop and then just add logic
#for (int i = 1; i < selection.Count; i++)
{
var item = selection[i];
<div class="row team">
<div class="col-sm-3">
<div class="thumbnail-style">
#if (i % 5 == 0)
{
<div class="col-sm-3">
<h4 >D #item</h4> <!--your actual html code-->
</div>
}
else
{
<h4 style="color:red">D #item</h4> <!--your actual html code-->
}
</div>
</div>
</div>
}

Cannot apply indexing with [] to an expression of type 'HttpRequest'

I am trying to get a value from a textbox of my View.
This is my View:
#model MyDataIndexViewModel
#{
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<h1>Meine Daten</h1>
</div>
</div>
var item = Model.User;
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6 myDataTitle">Email</div>
<div class="col-xs-6 col-sm-6 col-md-6">
#Html.TextBox("txtEmail", "", new { placeholder = item.Email})
</div>
</div>
}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<a class="btn btn-default pull-right" href="/ChangeMyData/Save">Speichern</a>
</div>
</div>
This is my Controller:
[HttpPost]
public ActionResult Save()
{
var email = Request["txtEmail"].ToString();
return View();
}
I get the error just as it says in the Title.
Thank you in advance!
VIEW:
#model MyDataIndexViewModel
#using (Html.BeginForm("Save", "CONTROLLER_NAME"))
{
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<h1>Meine Daten</h1>
</div>
</div>
var item = Model.User;
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6 myDataTitle">Email</div>
<div class="col-xs-6 col-sm-6 col-md-6">
#Html.TextBox("txtEmail", "", new { placeholder = item.Email, id="txtEmail"})
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<a class="submit btn btn-default pull-right">Speichern</a>
</div>
</div>
}
CONTROLLER
[HttpPost]
public ActionResult Save()
{
var email = Request.Form["txtEmail"].ToString();
return View();
}
You can either use strongly-typed viewmodel binding:
View
#model MyDataIndexViewModel
#* other stuff *#
#Html.TextBox("txtEmail", Model.Email)
Controller
[HttpPost]
public ActionResult Save(MyDataIndexViewModel model)
{
var email = model.Email;
return View();
}
Or use a TextBoxFor directly for model binding:
#model MyDataIndexViewModel
#* other stuff *#
#Html.TextBoxFor(model => model.Email)
Or if you still want to use HttpRequest members, Request.Form collection (a NameValueCollection) is available to retrieve text from txtEmail input:
[HttpPost]
public ActionResult Save()
{
var email = Request.Form["txtEmail"].ToString();
return View();
}
Note that Request["txtEmail"] is discouraged due to no compile time safety applied for it, because the key value may retrieved from Request.QueryString, Request.Form or other HttpRequestBase members.
Similar issue:
MVC TextBox with name specified not binding model on post
Access form data into controller using Request in ASP.NET MVC

Render Partial View in Layout MVC 4 with Caching and Data

I'm new to MVC.NET and I'm working with MVC4 .
In a project I should return System Information like Post count, User counts and etc. In My _Layout I have defined a Sidebar which will show these data.
here is my sidebar div
<div class="sideElement">
<div class="header">
مشخصات سیستم
</div>
<div class="body">
#{Html.Action("GatherSystemInfo","Home").ToString();}
</div>
</div>
My GatherSystemInfo Action is Like This :
Of Course I have used static data For testing
[OutputCache(Duration=900, VaryByParam = "none")]
[ChildActionOnly]
public ActionResult GatherSystemInfo(SystemInformation model = null)
{
model.QuestionCount = 10930;
model.ToturialCount = 10353;
model.UserCount = 120123;
model.ProjectsNumber = 10231;
model.DateTime = DateTime.Now.ToLongTimeString();
model.OnlineUsers = 28;
return PartialView("_SystemInfo", model);
}
And My Partial View Is like this:
#model Persiangeeks.Models.SystemInformation
<div class="infoContainer">
<div class="rightFloated sysInfoTitle">تعداد سوالات ثبت شده : </div>
<div class="leftFloated sysInfoValue">#Html.Raw(Model.QuestionCount)</div>
<div class="clear" ></div>
</div>
<div class="infoContainer">
<div class="rightFloated sysInfoTitle">تعداد آموزشهای موجود : </div>
<div class="leftFloated sysInfoValue">#Html.Raw(Model.ToturialCount)</div>
<div class="clear" ></div>
</div>
<div class="infoContainer">
<div class="rightFloated sysInfoTitle">تعداد پروژه های موجود : </div>
<div class="leftFloated sysInfoValue">#Html.Raw(Model.ProjectsNumber)</div>
<div class="clear" ></div>
</div>
<div class="infoContainer">
<div class="rightFloated sysInfoTitle">تعداد کاربران ثبت شده : </div>
<div class="leftFloated sysInfoValue">#Html.Raw(Model.UserCount) نفر</div>
<div class="clear" ></div>
</div>
<div class="infoContainer">
<div class="rightFloated sysInfoTitle">تعداد کاربران آنلاین : </div>
<div class="leftFloated sysInfoValue">#Html.Raw(Model.OnlineUsers) نفر</div>
<div class="clear" ></div>
</div>
But When I run my project I have Nothing to show in my sidebar div. here is the Image
Thank you for your help :)
Html.Action() – Outputs string
Html.RenderAction() – Renders directly to response\
Solution : use RenderAction() in place of Action()

Unobtrusive ajax - View being fully replaced by partial view

I have searched multiple examples and have tried several things to get this to work properly. Currently I am trying to get a div to be replaced by using an Ajax.HtmlLink pointing to a PartialViewResult method.
I have tried
Checking network and javascript consoles to make sure my .js are all being loaded
Unbundling the stock jqueryval bundle and including the scripts manually
Double checked I didn't have another DOM element with the same update target Id
Checked my ajaxoptions to ensure everything was correct
I know unobtrusive is working because my Email duplicate remote validation check is working.
I will now show some screen shots of my debugging process
Here is what the page looks like prior to ajax load
My goal is to replace the container on the right that is currently holding email, name, and zipcode values
Here is the result##
This is what my view code looks like
<div class="container">
<br />
<br />
<div class="row">
<div class="col-sm-3">
<div class="sidebar-account">
<div class="row ">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">My Account</div>
<div class="panel-body">
<ul class="nav">
<li>
<a class="active" href="account_dashboard.html">Me</a>
</li>
<li>
#Ajax.ActionLink("Alerts", "ManageAlerts", null, new AjaxOptions
{
HttpMethod = "GET",
UpdateTargetId = "ajax-update",
InsertionMode = InsertionMode.Replace,
OnBegin = "ajaxBegin",
OnFailure = "ajaxFail",
OnComplete = "ajaxComplete",
OnSuccess = "ajaxSuccess"
}, new { #class = "active" })
</li>
<li>
<a class="active" href="account_account.html">Linked Accounts</a>
</li>
<li>
<a class="active" href="account_ads.html">Manage ads</a>
</li>
<li>
<a class="active" href="account_ad_create.html">Create new ad</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="row hidden-xs">
<div class="col-lg-12">
<div class="well">
<div class="row ">
<div class="col-lg-3">
<img src="css/images/icons/Crest.png" width="45" />
</div>
<div class="col-lg-9">
<h4 style="margin-top: 0">Increase visibility</h4>
<p>Don't forget to 'bump' your listing to gain more visibility</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-9">
<div id="ajax-update">
#Html.Partial("_ManageUserAccount", Model)
</div>
</div>
<br />
</div>
Partial View Code##
<div id="fadein-div">
<div class="panel panel-default">
<div class="panel-heading">#Model.Username : Alerts</div>
<div class="panel-body">
<br />
<div class="row">
<div class="col-sm-12">
<p><span style="font-weight:bold">Change your alerts!</span></p>
</div>
</div>
<br />
#using (Html.BeginForm("ManageAlerts", "Account", FormMethod.Post, new { #class = "form-vertical", role = "form" }))
{
#Html.AntiForgeryToken()
<fieldset>
<div class="row">
<div class="col-sm-12 ">
<div class="form-group">
<div class="row">
<div class="col-sm-2">
<div class="checkbox">
#Html.CheckBoxFor(P => P.EmailAlertsOn) #Html.LabelFor(P => P.EmailAlertsOn)
</div>
</div>
</div>
</div>
<div id="control-div">
<div class="form-group">
<h3>Email alert options</h3>
</div>
<div class="form-group">
<div class="checkbox">
#Html.CheckBoxFor(P => P.EmailOnNewMessageOn) #Html.LabelFor(P => P.EmailOnNewMessageOn)
</div>
<div class="text-danger">
#Html.ValidationMessageFor(P => P.EmailOnNewMessageOn)
</div>
</div>
<div class="form-group">
<div class="checkbox">
#Html.CheckBoxFor(P => P.EmailOnReplyOn) #Html.LabelFor(P => P.EmailOnReplyOn)
</div>
<div class="text-danger">
#Html.ValidationMessageFor(P => P.EmailOnReplyOn)
</div>
</div>
<div class="form-group">
<div class="checkbox">
#Html.CheckBoxFor(P => P.EmailOnAccountUpdateOn) #Html.LabelFor(P => P.EmailOnAccountUpdateOn)
</div>
<div class="text-danger">
#Html.ValidationMessageFor(P => P.EmailOnAccountUpdateOn)
</div>
</div>
</div>
<br />
<button type="submit" class="btn btn-primary">Save details</button>
</div>
</div>
</fieldset>
}
</div>
</div>
And finally, controller code##
[HttpGet]
public PartialViewResult ManageAlerts()
{
var user = UserManager.FindById(Guid.Parse(User.Identity.GetUserId()));
Models.Account.ManageAlertsViewModel vm = new ManageAlertsViewModel();
vm.Username = user.UserName;
vm.EmailAlertsOn = user.EmailAlertsOn;
vm.EmailOnAccountUpdateOn = user.EmailOnAccountUpdateOn;
vm.EmailOnNewMessageOn = user.EmailOnNewMessageOn;
vm.EmailOnReplyOn = user.EmailOnReplyOn;
return PartialView("_ManageAlerts", vm);
}
<!-- js library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script>
<script src="~/Themes/Authenticity%20Forms/Authenty/js/bootstrap.min.js"></script>
<script src="~/Themes/Authenticity%20Forms/Authenty/js/jquery.icheck.min.js"></script>
<script src="~/Themes/Authenticity%20Forms/Authenty/js/waypoints.min.js"></script>
<!-- authenty js -->
<script src="~/Themes/Authenticity%20Forms/Authenty/js/authenty.js"></script>
<!-- preview scripts -->
<script src="~/Themes/Authenticity%20Forms/Authenty/js/preview/jquery.malihu.PageScroll2id.js"></script>
<script src="~/Themes/Authenticity%20Forms/Authenty/js/preview/jquery.address-1.6.min.js"></script>
<script src="~/Themes/Authenticity%20Forms/Authenty/js/preview/scrollTo.min.js"></script>
<script src="~/Themes/Authenticity%20Forms/Authenty/js/preview/init.js"></script>
#Scripts.Render("~/bundles/jqueryval")

Using nested beginform in MVC

I have scenario where I have load data on dropdown selection change and after manipulation I have to submit the page for saving.
I have developed the page but since I new to MVC so can anyone verify me code and can suggest better approach of doing the same.
#using (Html.BeginForm("Save", "forecast"))
{
using (Html.BeginForm("YearChange", "forecast", FormMethod.Get, new {#id="frmYearChange" }))
{
<div class="span12">
<div class="well well-sm" style="padding-bottom: 0px !important;">
<div class="span5">
<div class="control-group">
<label class="control-label" for="selectGroup">
Select Group:</label>
<div class="controls">
#Html.DropDownListFor(m => m.groupId, Model.groupList, "--Select Group--", new { #id = "selectGroup" })
</div>
</div>
</div>
<div class="span5">
<div class="control-group">
<label class="control-label" for="selectYear">
Select Year:</label>
<div class="controls">
#Html.DropDownListFor(m => m.yearId, Model.yearList, "--Select Year--", new { #id = "selectYear", onchange = "selectYear_indexChanged();" })
</div>
</div>
</div>
<div class="clearfix">
</div>
</div>
</div>
}
.....
}
You can't have nested forms. You can however have multiple forms per view.
See W3C for more info.

Resources