I have a view that loads a set of modules (we will say other partial views) when the view renders.
So think of it like this in the master view:
<div>
#Html.PartialView("~/Views/MyApp/Partials/Module1.cshtml");
</div>
<div>
#Html.PartialView("~/Views/MyApp/Partials/Module2.cshtml");
</div>
<div>
#Html.PartialView("~/Views/MyApp/Partials/Module3.cshtml");
</div>
There is a model value that is altered in the partial view Module2.cshtml. Actually it is altered in the action for the Module2 view. I am setting the model value in public ActionResult RenderChecklist(Model pageModel, IGrouping<string, ChecklistItem> list, int checklistCount):
if (itemCounter == itemCheckedCounter)
{
priorityCounter++;
pageModel.MyAppInfo.ChecklistPriorityLevel = priorityCounter;
listRestrict = "no-restrict";
overlayShow = "hidden";
}
else
{
listRestrict = "no-restrict";
overlayShow = "hidden";
}
Depending on the ChecklistPriorityLevel value is determines if an overlay is shown in Module1, Module3, etc., but since Module1 loads before Module2, the value of of ChecklistPriorityLevel in Module1 is always initiated at 0.
The code in the partial view that is called in each module is something like this:
#if (moduleRestrict && !(moduleRestrictPriorityLevel <= checklistPriority) && !Model.GetValue<bool>("moduleRestrictBypass"))
{
const string moduleLockMessage = "This section is locked.";
<div class="module overlay show">
<img src="/assets/myapp/images/lock.png" alt="Module Lock">
<p>#moduleLockMessage</p>
</div>
}
The relative code in the model is just a regular get, set at this moment:
namespace MyApp.Core.MySite.Models
{
/// <summary>
/// Model for MySite.
/// </summary>
public class MyAppInfoModel
{
... //other models
[Ignore]
public int ChecklistPriorityLevel { get; set; }
}
}
So my question is how do I get the change in the value of this model to trigger the change in other modules (partial views) that have already loaded?
DISCLAIMER: I changed some of my actual code for privacy purposes. I am just trying to give enough information to have viewers understand what I am trying to do. I am looking for the best option, whether it is asynchronous, or whatever, to properly get the value in other partial views regardless of which partials load first.
Fix for my own issue (But I would still really like to see how this could be handled)
So for my particular problem I decided that I would just force the value of the model to be set before loading any of the modules. The way my app works, Module2 could be in any spot actually, and any modules could be ahead or behind Module2. What order the modules are in is determined in the backoffice. So I just decided to create a SurfaceController and get the data (checklists) on the main view, but then I have to get the data again in "Module2", which is my ChecklistModule. I don't really like having to iterate the checklists twice, but in order to get that ChecklistPriorityLevel value I have to iterate through the checklists.
So in my main view a call the following:
MyAppChecklistController.SetChecklistPriorityLevel(Model);
Then my method is:
public static void SetChecklistPriorityLevel(MyAppRenderModel pageModel)
{
var checklist = GetCheckList(pageModel);
var priorityCounter = 1;
foreach (var list in checklist)
{
var listCount = list.Count();
var itemData = new ValueTuple<int, int, string>(1, 1, null);
itemData = list.OrderBy(x => x.IsChecked).ThenBy(x => x.SortOrder).Aggregate(itemData,
(current, item) => GetItemList(item, current.Item1, current.Item2, current.Item3, listCount));
var itemCounter = itemData.Item1;
var itemCheckedCounter = itemData.Item2;
// var itemList = itemData.Item3;
priorityCounter = GetPriorityLevel(itemCounter, itemCheckedCounter, priorityCounter);
pageModel.ChecklistPriorityLevel = priorityCounter;
}
}
Then when I render the checklist in the ChecklistModule partial view:
[ChildActionOnly]
public ActionResult RenderChecklist(IGrouping<string, ChecklistItem> list,
int checklistCount, int checklistCounter, int priorityCounter)
{
var parentFolder = list.First().UmbracoContent.Parent;
var listCount = list.Count();
var itemData = new ValueTuple<int, int, string>(1, 1, null);
var color = GetChecklistColorValue(parentFolder);
itemData = list.OrderBy(x => x.IsChecked).ThenBy(x => x.SortOrder).Aggregate(itemData,
(current, item) => GetItemList(item, current.Item1, current.Item2, current.Item3, listCount));
var itemCounter = itemData.Item1;
var itemCheckedCounter = itemData.Item2;
var itemList = itemData.Item3;
var checklistDict = GetChecklistRestrictions(parentFolder, itemCounter,
itemCheckedCounter, priorityCounter);
checklistDict.Add("color", color);
checklistDict.Add("itemList", itemList);
checklistDict.Add("checklistCounter", checklistCounter);
return GetChecklistLayout(checklistCount, checklistLayoutDict);
}
So as you can see I am pretty much running through the checklist twice in one page load. I didn't want to have to do that. If anyone has a better idea to make this more efficient, let me know.
UPDATE: The above solution (even though only part of the code is posted) fixed my issue, however I decided that I would just catch the checklist on the view and add it to the models too, then use that model (pageModel.Checklists) in the partial, so I am not actually grabbing the checklist twice with Linq. I still have to iterate through the checklists twice, but I am not grabbing the values twice. So still more of a hack, but maybe I will keep finding a way later to streamline this.
Related
Hello could you please help with this. I'm using asp.net mvc 5 application, my home view has 2 parts. One where the data shown and the other which filters this data. For now I just show all my data without filtering. I've read few tutorials, but they use fewer properties for filtering. In my case I have really large data for filtering (more than 10 properties).
So I've found recommendation to use viewmodel as the parameter for filter, but there weren't any suggestions how to implement view and controller.
I've made some preparation for this work, I've took all the logic from the controller and created separate class called business logic and put there following code:
public IQueryable<ModelDTO> GetData(FilterViewModel filter)
{
var main = (from data in db.Table
// Other commands
select new
{
data.Id,
data.Title,
data.Creteria1,
data.Creteria2,
// Other fields
}).ToList();
// Other logic
if (filter != null)
{
// TODO develop the filter logic
if (filter.Creteria1.HasValue)
{
main = main.Where(x => x.Creteria1 == filter.Creteria1);
}
if (filter.Creteria2.HasValue)
{
main = main.Where(x => x.Creteria2 == filter.Creteria2);
}
// etc.
}
return main.AsQueryable();
}
My controller looks like this:
public ActionResult Index(FilterViewModel f)
{
var b = new BusinessLogic();
var m = b.GetData(f);
return View(m);
}
In my view I've implemented html forms with all fields, how to map it to FilterViewModel?
I need whenever user change fields in filter form and hit submit button to filter data.
I need to have by default filled FilterViewModel, so initially it will loads all the data.
Also in my view I'm using DTOModel, so I can't just my FilterViewModel.
I have an Ajax button that whenever I click it, it shows a single record from the database (in my Controller I used .Take(1) )
public PartialViewResult BtnNext()
{
List<Queue> model = db.Queues.OrderBy(x => x.QueueNumber).Take(1).ToList();
return PartialView("_queuenumber", model);
}
What I would like to do here is - whenever I click the next button it will display the first record from the database, then when I click it again it will show the second record and so on..
I wonder if that is even possible and what kind of stuff should I use to do that?
Yes. Its possible.
Just set Application["counter"] = 0 in Application_Start function then make value increments by 1 in result view and use it to get next record.
public PartialViewResult BtnNext()
{
List<Queue> model = db.Queues.OrderBy(x => x.QueueNumber).Skip(Application["counter"]).Take(1).ToList();
Application["counter"] = Application["counter"] + 1;
return PartialView("_queuenumber", model);
}
Reference
Use FormCollection try following code.
public PartialViewResult BtnNext(FormCollection Form)
{
Int32? Count = Convert.ToInt32(Form["Count"]??0);
List<Queue> model = db.Queues.OrderBy(x => x.QueueNumber).ToList();
model.ElementAt(count); // [NotMapped] public Int32? count { get; set; } add in model class
model.count=count+1;
return PartialView("_queuenumber", model);
}
on view
<input type="submit" class="btn btn-primary" value="Save" id="BtnNext">
<input type="hidden" id="Count" name="Count" value="#Model.Count" />
A good practice when you realize your Views need to handle and manipulate your data, is to create a ViewModel class that wraps all the objects that you need to send to that view. In your case, you can start with a simple
public class QueueViewModel
{
public Queue Queue { get; set ; }
public int CurrentRecord { get; set ; }
}
Now, all you have to do is changing the action method the controller so that you initialize and pass the ViewModel to the View. It will also be better to have an optional argument acting as the default record, and then using the linq instruction Skip to go to and take a specific record:
Public PartialViewResult NextRecord(int current = 0)
{
QueueViewModel model = new QueueViewModel();
model.CurrentRecord = current;
model.Queue = db.OrderBy(x => yourClause).Skip(current).Take(1);
return PartialView(“yourview”, model);
}
I changed the List<Queue> within your model as I think you don’t need a list if you’re only showing one record at a time, but you can easily go back to the generics if you feel you really need to.
As for the view part where you handle the index on the model, there are many ways to achieve the same result. What I personally like to do is using the model to fill a data attribute of a DOM element and use that in the Ajax call. Since you now have
#model yourModelNamespace.QueueViewModel
it is possible for you to set an element (let’s say a button) to host the current value:
<button data-current-record=“#Model.CurrentRecord”>...</button>
You can now very easily retrieve that value within your Ajax call to the action method:
var currentRecord = parseInt($(‘button’).data()[currentRecord]);
$.ajax({
url: yourPathToTheAction,
type: ‘GET’,
data: {
current: currentRecord + 1
}
});
This way you can go further and add other functions calling the same controller to move to previous record or jump to the last or the first and so on...
I have a Partial View for showing the list of products. In this view I have ActionLinks above each column to handle sorting. There are total 4 of them and they are all identical to below one:
<div class="col-xs-2">
#Html.ActionLink(Html.DisplayNameFor(m => m.ProductName).ToHtmlString(), "Products", AppendQueryString.AppendQuery("sortOrder", ViewData["ProductNameSort"].ToString()))
</div>
In order to be able to append the current URL without losing any previously querystrings, I created a static class AppendQueryString and a method in it called AppendQuery. The class looks like below:
public static RouteValueDictionary AppendQuery(string key, string value)
{
RouteValueDictionary routeValues = new RouteValueDictionary();
var context = HttpContext.Current;
var req = context.Request.QueryString;
foreach (string qkey in req)
{
routeValues[qkey] = req[qkey];
}
if (!routeValues.ContainsKey(key))
{
routeValues.Add(key, value);
}
else
{
routeValues.Remove(key);
routeValues.Add(key, value);
}
return routeValues;
}
First I didn't realize what was the problem but something was trying to rewrite the same key value sent into this method so I had to use that ugly if-else there to add the key-value into the RouteValueDictionary properly and continue on with the project.
However, now the problem is obvious that for some reason, on page loading these four ActionLinks I have are being fired one after another so the AppendQuery method is being called 4 times on page load. The main View that renders this partial view is being called only once I am sure about it. Any of you have any idea why is this happening ? Thanks in advance.
My goal:
The user session will keep track of guid's stored in
Session.Add(guid.tostring()).
When a partial is refreshed inside a div, the controller will check
for existing guids. This will let me track what notifications need
displayed, what are already being displayed to avoid duplicates ..etc.
The Problem:
When a notification should be displayed again, it isn't even though I
see it in the model being passed to the view
What I think is the cause
For some reason when I debug the controller as the very start of the
method to load the partial, it's being loaded many times which I
believe is why when a notification should be displayed, it isn't.
Main Index View that refreshes the partial. #overview. Also the interval is every 15 seconds.
function intervalTrigger() {
$('#overview').load('/Home/Overview');
};
<div id="overview">
#{Html.RenderPartial("Overview", "Home");}
</div>
Code inside Overview partial that displays the alerts
#for (int i = 0; i < Model.DisplayAlerts.Count(); i++)
{
#:$(function () {
#:$.Notify({
#:keepOpen: true,
#:caption: '#Model.DisplayAlerts[i].SectionName',
#:content: '#Model.DisplayAlerts[i].ParamDescription <br/> #Model.DisplayAlerts[i].DetailDatetime.ToString("MM/dd/yyyy hh:mm:ss tt ")',
#:type: 'alert'
#:});
#:});
}
Code that is populating the model being handed to the view
OverviewModel model = new InformationController().GetOverviewModel();
model.DisplayAlerts = new List<BasicSectionDetailModel>();
List<BasicSectionDetailModel> details = new Collector.InfoCollector().GetSectionDetailsNotOKState();
List<Guid> sessionKeys = new List<Guid>();
for (int i = 0; i < Session.Contents.Count; i++)
{
Guid guid;
if (Guid.TryParse(Session.Keys[i].ToString(), out guid))
{
sessionKeys.Add(guid);
}
}
List<BasicSectionDetailModel> addAlert = details.Where(x => !sessionKeys.Any(x2 => x2 == x.ID)).ToList();
foreach (BasicSectionDetailModel alert in addAlert)
{
Session.Add(alert.ID.ToString(), null);
}
List<Guid> removeAlert = sessionKeys.Where(x => !details.Any(x2 => x2.ID == x)).ToList();
foreach (Guid remove in removeAlert)
{
Session.Remove(remove.ToString());
}
model.DisplayAlerts = addAlert;
return model;
I found the issue
model.DisplayAlerts = addAlert;
addAlert was being destroyed. I had to copy the contents into the model and not assign one var to another.
I have been introduced to Razor as applied with MVC 3 this morning, so please forgive me if my question seems terribly uninformed!
I am working with an app whose workflow involves allowing a user to select a value (warehouse) from a drop down list, and add a record (material) from that warehouse to another record (Materials Request). Once the first material has been added to the Materials Request, I need to permanently set the value of the drop down to the warehouse that was first selected, then disable the drop down control (or set to read only, perhaps). The existing code in the razor file uses the DropDownListFor() method, including a ViewBag collection of Warehouse records. I have seen discussions which suggest abandoning the ViewBag design, but honestly I don't have the desire to rewrite major portions of the code; at least it looks like a major rewrite from the perspective of my experience level. Here's the original code:
#Html.LabelPlusFor(m => m.WarehouseId, "*:")
#Html.DropDownListFor(m => m.WarehouseId, (IEnumerable<SelectListItem>)ViewBag.WarehouseCodes, "")<br />
I believe I have been able to select a value based on a session object, though I'm still not sure how to disable the control. Here's my change:
#{
int SelectedWarehouseId = -1;
if (HttpContext.Current.Session["SelectedWarehouseId"] != null)
{
SelectedWarehouseId = Int32.Parse(HttpContext.Current.Session["SelectedWarehouseId"].ToString());
}
}
#Html.LabelPlusFor(m => m.WarehouseId, "*:")
#{
if (SelectedWarehouseId > -1)
{
#Html.DropDownListFor(m => m.WarehouseId, new SelectList((IEnumerable<SelectListItem>)ViewBag.WarehouseCodes, "WarehouseId", "WarehouseDescription", (int)SelectedWarehouseId))<br />
}
else
{
#Html.DropDownListFor(m => m.WarehouseId, (IEnumerable<SelectListItem>)ViewBag.WarehouseCodes, "")<br />
}
}
When the material is added to the Material Request, the WarehouseId is passed to the controller and I can access that value as "model.WarehouseId" in the controller class. However, I'm not sure how to get that value back to the View (apologies for the large code block here):
[HttpPost]
[TmsAuthorize]
public ActionResult Create(ItemRequestViewModel model)
{
string deleteKey = null;
//Removed code
else if (Request.Form["AddToRequest"] != null)
{
// If the user clicked the Add to Request button, we are only
// interested in validating the following fields. Therefore,
// we remove the other fields from the ModelState.
string[] keys = ModelState.Keys.ToArray();
foreach (string key in keys)
{
if (!_addToRequestFields.Contains(key))
ModelState.Remove(key);
}
// Validate the Item Number against the database - no sense
// doing this if the ModelState is already invalid.
if (ModelState.IsValid)
{
_codes.ValidateMaterial("ItemNumber", model.ItemNumber, model.WarehouseId);
Session["SelectedWarehouseId"] = model.WarehouseId;
}
if (ModelState.IsValid)
{
// Add the new Item Request to the list
model.Items.Add(new ItemViewModel() { ItemNumber = model.ItemNumber, Quantity = model.Quantity.Value, WarehouseId = model.WarehouseId });
ModelState.Clear();
model.ItemNumber = null;
model.Quantity = null;
model.WarehouseId = null;
}
}
//Removed code
return CreateInternal(model);
}
private ActionResult CreateInternal(ItemRequestViewModel model)
{
if (model != null)
{
if (!String.IsNullOrEmpty(model.SiteId))
{
ViewBag.BuildingCodes = _codes.GetBuildingCodes(model.SiteId, false);
if (!String.IsNullOrEmpty(model.BuildingId))
ViewBag.LocationCodes = _codes.GetLocationCodes(model.SiteId, model.BuildingId, false);
}
//Removed code
}
//Removed code
ViewBag.WarehouseCodes = _codes.GetWarehouseCodes(false);
return View("Create", model);
}
So my questions are, how do I disable the drop down list, and how can I pass a value for the selected WarehouseId back to the view? I've also considered adding the value to the ViewBag, but to be honest I don't know enough about the ViewBag to recognize any unintended consequences I may face by just randomly modifying it's contents.
Thanks for any help offered on this.
Without going into which approach is better...
Your dropdown should be rendered as an HTML select element, in order to disable this you'll need to add a disabled="disabled" attribute to it.
The DropDownListFor method has a htmlAttributes parameter, which you can use to achieve this:
new { disabled = "disabled" }
when your pass model to your view like
return View("Create", model);
if WareHouseID is set in model then
Html.DropDownListFor(x=>x.WareHouseID, ...)
will automatically set the selected value and u don't have to do that session processing for this. So far as disabling a field is required, stewart is right. you can disable drop down this way but then it won't be posted to the server when u submit the form. you can set it to readonly mode like
new{#readonly = "readOnly"}