I'm new with MVC and I need to put into an iframe a view which returns the content of a page( styles/css,text/html,images etc) and the view to containt the main menu of the application, from the Index view.
Can anyone help me with this?
The code line which returns the content in the view is :
#Html.Raw(System.Text.Encoding.ASCII.GetString(#Model.Content))
Just try to create controller with action that returns your view.
public class MyController
{
public ActionResult Index()
{
return View();
}
}
index view content:
#Html.Raw(System.Text.Encoding.ASCII.GetString(#Model.Content))
After that
<iframe src="localhost/mycontroller">
</iframe>
Related
I want to redirect a page with few parameters. However, if I try to do it by returning RedirectToAction(), the page will not work. Here is the code:
Controller Home, Action Display:
public IActionResult Display()
{
return RedirectToAction("Display", new
{
token = "2a4d237b-e2db-4ffe-ae42-787ae4e7566c",
id = "abcdefgh",
display = "Default"
});
}
View
<a asp-area="" asp-controller="Home" asp-action="Display">Link</a>
If I return View() in the Display() method in the Home controller, that wotks fine the page is loaded.
public IActionResult Display()
{
return View(); //this works
}
not sure what you want to achieve. If you want to redirect to an action in the same controller, you need to specify the name of your action that returns the view.
In your code above, you are redirecting to the same action. Have a look at this Answer here
Am trying to get a partial view to render with an AJAX call. The following ActionResult is in my base controller which is inherited by all other controllers in the solution:
public ActionResult FileManager()
{
return View("_FileManagerPartial");
}
and the folowing code is another partial that sits on the page
#Ajax.ActionLink("File Manager", "FileManager", new AjaxOptions { UpdateTargetId = "dvFilemanagerContainer" });
dvFilemanagerContainer is a div in the layout view and the partial view "_FileManagerPartial.cshtml" is in the shared views folder.
When I click the link for the ajax call, instead of loading the intended partial view it loads a duplicate of the page into the div.
Any ideas?
Edit
PartialView contents its currently just the following
<div id="dvFilemanagerWrapper">
File Manager
</div>
change your controller to
public PartialViewResult FileManager()
{
return PartialView("_FileManagerPartial");
}
In View add this line, so that with partial view, the master layout is not rendered, only partial view is rendered:
#{
Layout = null;
}
<div id="dvFilemanagerWrapper">
File Manager
</div>
Problem was to do with how my RouteConfig was handling the call. Have created a new MapRoute to point it to the right place and is now working. Thanks for help guys.
I have a controller and one of its methods (actions) access my database of items. That method checks the item type. How do I show my partial view only if the item retrieved from my database is of a specific type?
Controller Action example:
public ActionResult CheckItem(Koko model)
{
var item = db.Items.Where(item => item.Number == model.Number).First();
if(item.Type=="EXPENSIVE")
{
//show partial view (enable my partial view in one of my Views)
}
}
You could return a PartialView action result:
public ActionResult CheckItem(Koko model)
{
var item = db.Items.Where(item => item.Number == model.Number).First();
if (item.Type=="EXPENSIVE")
{
return PartialView("name of the partial", someViewModel);
}
...
}
Now the controller action will return partial HTML. This obviously means that you might need to use AJAX in order to invoke this controller action otherwise you will get the partial view replace the current browser window. In the AJAX success callback you could reinject the partial HTML in the DOM to see the update.
I have an MVC3 application that I am implementing pjax into . Everything is working well except what to do on the server side when an address gets loaded that doesn't already have the main view on the client side. My Controller code looks like
public virtual ActionResult Details(Guid id)
{
var partDetail = new PartDetail(id);
var partialView = PartialView("Details", partDetail);
if(Request.Headers["X-PJAX"]!= null)
{
return partialView;
}
var mainView = View("Index");
// Stick Partial View into main view at #update_panel?
return mainView;
}
How can I stick the partial View into the main view so it inserts the partial view in the #update_panel?
Ok, without a major refactor, you could do the following.
(this assumes that you are able to set the #model on index.cshtml to PartDetail()).
in your controller action above, change:
var mainView = View("Index");
to:
var mainView = View("Index", partDetail);
then, inside your index.cshtml, add the following:
<div id="update_panel">#RenderPartial("Details", Model)</div>
As i said, this will ONLY work if the index #model is set to PartDetail(), otherwise, a little refactoring on the model in the index view will be required to include this PartDetail() model. this viewmodel might well look like the following:
public class IndexViewModel
{
ModelForIndex Index{get; set;}
PartDetail Details{get; set;}
}
this refactored viewmodel would be added to the index.cshtml as #model IndexViewModel and consumed by the partial as:
<div id="update_panel">#RenderPartial("Details", Model.Details)</div>
hope this makes sense.
I created a custom Model i.e. to support my Razor View. Then I created a controller as following`namespace MyCandidate.Controllers
public class CandidateViewModelController : Controller
{
//
// GET: /CandidateViewModel/
public ActionResult Index()
{
return View();
}
}
I also have the following statement in my _Layout.cshtml
#Html.ActionLink("Canid", "Index", "CandidateViewModel")
Next i created a view and the very first statement of the view is
#model MyCandidate.Models.CandidateViewModel
when i run my project i get the following error
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
I spent more than 3 hours but could not figure out?
your Index() not get any parameters, but you send "CandidateViewModel") add Index(string input) method with attribute [HttpGet] to controller.
this error meen you haven't view "Index" in "Views/CandidateViewModel/Index.cshtml".
Maybe you delete master page files (_ViewSrat, _Layout)
Or you made a mistake when you change your routes
Change your ActionLink to:
#Html.ActionLink("Canid", "Index")
If you want to pass any data to View , you can also use ViewBag:
// Controller :
ViewBag.CandidateValues = CandidateViewModelData;
// View
#Html.Label("LabelName", (CandidateViewModel) ViewBag.CandidateValues.FiledName);