I have an index view which return a list of data. The title parameter contains HTML tags. When I load this view, my title like that <strong>title</strong>.
But I don't need theses tags in this situation, so I'm trying to remove them only for this view. I have read that operation could be done in controller, could you tell me how to do that with HtmlAgilityPack ?
Controller
public ActionResult ArchivePanel(string sortOrder, string currentFilter,string searchString,int? page)
{
...
var alertMap = db.AlertMap.Include(a=>a.AlertLog).Include(a=>a.AlertMode).Include(a=>a.AlertPriority).Include(a=>a.AspNetUsers);
...
return View(alertMap.ToPagedList(pageNumber.pageSize));
}
I need to do this operation on a.AlertLog.AlertTitle value
View
<td>
#Html.DisplayFor(modelItem=>item.AlertLog.AlertTitle)
</td>
Thanks
I would advise you not to use the HtmlAgilityPack for this. This is for manipulating HTML, and in the controller you should not be changing the raw HTML.
Instead, I would suggest using a different view for your page in this situation, that had the look and feel you need in this context. You can factor out the parts of the view that are common with the original view into a shared layout.
Related
I'm trying to share data from my main _Layout to the views rendered by RenderBody.
I tried to use a ViewBag initialized in "Shared/_Layout" but it doesn't work. However, I noticed that this ViewBag can be used in partials but not in the views displayed by #RenderBody.
Example _Layout:
PageData["XX"] = "myValue";
ViewContext.Controller.ViewBag.XX= "myValue";
Page.XX= "myValue";
ViewBag.XX= "myValue";
ViewContext.ViewData["XX"] = "myValue";
<body>
#Html.Partial("_MyPartial") // Everything has the correct value in the partial
<main class="content">
#RenderBody() // Everything is null in that view
</main>
</body>
I tried with ViewBag.Property, ViewData["Property"], Page.Property and PageData["Property"] but they are all null in the view rendered by RenderBody.
It seems that because the Layout is the last element compiled.
I don't want to use any code. Everything needs to be in the view, do something pretty similar to my example above. It must be in the layout because the value I want to share is defined by the user through the CMS. It means that this layout is used by static page but also views rendered by controller.
My question is: How I can give/share data from the _Layout to my views rendered by RenderBody()?
try this in your Layout :
#ViewContext.Controller.ViewBag.Test= "test";
How I can give/share data to my views rendered by RenderBody()?
Controllers are responsible for preparing the data (model, ViewBag, TempData, etc) and handing it over to Views.
You could also look into filters for preparing the data.
You need to populate your ViewBag in your Controller:
public class HomeController
{
public ActionResult Index()
{
// populate your ViewBag
ViewBag.myValue = "some text";
}
}
And then in your View/PartialView you can use it like this:
MyPartialView.cshtml
<label>#ViewBag.myValue</label>
Yes you can used viewBag in your partials view. When call viewBag in razor file(.cshtml)
you must be return a partial view for display.
ViewBag and ViewData is view-specific, so they're emptied whenever you hit a different View/Action. Try checking this link http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications for more info.
Try using TempData for that effect - http://www.tutorialsteacher.com/mvc/tempdata-in-asp.net-mvc
I want to modify the content of a div in my shared layout when there is an update at my database. Whats the best way to go about it ? Timers to check database + somehow update view from controller ? In view c# functions ? ajax ?(don't know much about it) or some other way ? Thanks in advance.
Use setTimeout utility of javascript to get timer behavior.
Create a Controller Action and its associated partial view which will have the content you need to load dynamically. You can return a formatted Html from the view or a structured Json object from the controller action whichever is suitable for you..
considering that your following is your DIV tag.
<div id="dynamicContent"></div>
use
$(function(){
var function updatecontent()
{
$("#dynamicContent").load(URL)
setTimeout(updatecontent,5000);
}
setTimeout(updatecontent,5000);
});
to load content dynamically, url would be
YourDomain.com/Controller/Action
$.load()
will load the content of partial view using Ajax, and you will get a nice behavior on page.
I render my ViewModel with
Create.cshtml:
#using (Html.BeginForm("Create","TestTemplate")) {
#Html.EditorForModel()
}
I do not like the output content because all the divs are one below the other. What if I want a horizontal layout where one div floats lefthandside to the other div?
How can I modify such layout changes using the EditorForModel() helper when there is no html in my Create.cshtml view ?
UPDATE:
Why do I have to create TWO views. In the one I call #Html.EditorForModel() in the other EditorTemplate-view I do all the layout stuff which I could also do within the first view via EditorFor( x => x....) ??? This seems strange to me.
You want to create an EditorTemplate. Basically create a new folder called EditorTemplates in ~/Views/Shared and then create a new view within that folder that has the name of your model, e.g. Template.cshtml or whatever.
When you call #Html.EditorForModel() it will use this view as the template.
The question is pretty old, but to my eyes there is another approach that should be mentioned. Although MVC is not MVVM try to think a little more in that pattern. Since the generated HTML is nothing more than the content of a page it will be used like a view model in the browser. It is not the job of HTML to Format your page. Try to modify the layout of your page by using CSS which will be used to render the view in the browser.
Just use a CSS selectors like the following to change the layout of your form, the input elements or just a specific input element:
form { your CSS here }
form input { your CSS here }
form input[name="NameOfInput"] { your CSS here }
In these days the power of CSS is nearly unlimited: Let your items flow left, right or alternating and so on ;-) You won't need to create two views either.
I am new to Stack Overflow and to ASP.NET MVC.
I have been asked to do a project where I want to use ASP.NET MVC, but I have some problems wrapping my head around it and I hope some of you could get me in the right direction.
The project is a kind of a search portal. On every page there is a dropdown box where you basicly select the dataset (it's based on books) you want to search. In the dropdown is the name of the book you want to search.
Of course there is also a search field. These 2 objects are on every page and has the same function on all pages, and I can't get these 2 objects to communicate.
I have these 2 in separate partialviews and want to generate an action for the search formfield something like this:
domain.com/{bookname}/search/{searchterm}: this is the thing created from the dropdown and the search box.
But can I do this in the searchfields partialview in some way, or do I have to grab these value in each controller?
I hope this makes any sense.
Create a partial view with your dropdown and textbox, using the BeginForm helper:
<% using(Html.BeginForm("Index", "Search")) %>
<% { %>
<%= Html.DropDownList("BookNames") %>
<%= Html.TextBox("SearchTerm") %>
<% } %>
Then in your SearchController's Index action, you should be able to grab the values from the form collection or using your dropdown's and textbox's id.
public ActionResult Index(FormCollection frmCollection)
{
// ...
// also you can redirect to another action/controller if you needed
// return RedirectToAction("...", "...");
}
or
public ActionResult Index(int bookNames, string searchTerm)
{
// ...
// also you can redirect to another action/controller if you needed
// return RedirectToAction("...", "...");
}
Hope this helps.
I have implemented a similar search requirement on one of my projects with ASP.NET MVC. However, I had the Dropdown and Search in a single Partial View.
I then used Javascript to pick up the selected values and redirect the user to the URL with pattern domain.com/{criteria}/search/{searchWord}.
This way I had to use a single controller to search, with a single view to view search results.
I have a customer index page that displays customer lists. I have a search functionality within this page and I want the url to be http://mysite/search?id=434 when i perform the search. The index page will also display the search result.
Thanks
public class CustomerController : Controller
...
public ActionResult Search(int id)
{
ViewData["SearchResult"] = MySearchBLL.GetSearchResults(id);
return View("Index");
}
...
Hope this helps
No, you shouldn't have. Just use View("ViewName"); in the controller to show the appropriate view in other actions.
In the URL that you suggest, you should have a Controller method called 'search' and using the 'index' view for that controller.
If thats the case, you can post it back to the same action and in the Controller have different sets of code for the 'GET' and 'POST' to give the functionality that you are looking for.
Have the HTML form post to the same action that rendered it. That action can decide if it's a non-search (first visit) or a search results rendering. That action can populate ViewData with the appropriate data. That is, if you want to do that.
You can also have two views, really easily. And the action can transparently decide which one to render.