Show view Page with dynamic html content from model property - c#

In my .Net MVC 3 application I need to make this functionality:
When user click on the button, it should navagate him into some page but with html content that I have in model property. Is it possible?

Yes, it is possible.
Use on this new view
#Model YourModel
#{
Layout = null;
}
#Html.Raw(Model.Propery)

Sure, inside the view:
#model MyViewModel
#{
Layout = null;
}
#Html.Raw(Model.SomePropertyThatContainsHtml)
But that's completely ridiculous, you'd rather have your controller action directly return ContentResult:
public ActionResult SomeAction()
{
MyViewModel model = ...
return Content(model.SomePropertyThatContainsHtml, "text/html");
}

Related

Model shows in Text in .CSHTML view

I have a cshtml view and in there I have used Model. But the problem is that model shows in my View.
#Model LMM.NEWS.Documents;
#{
Layout = null;
ViewBag.Title = "News Doc Download";
}
In my view, It shows me in the text like below. How to solve this?
System.Collections.Generic.List`1[LMM.Entities.DTO.NEWS.Documents] LMM.Entities.DTO.NEWS.Documents;
Here i have attached,my model shows in text in my view
Is because of this line:
#Model LMM.NEWS.Documents;
it's calling the toString(); method from Object, one solution is to override toString(); on your Documents class:
public override string ToString()
{
return string.Empty;
}
Not completely sure if that fixes your problem because I don't have a project to test it out, but you could try.

Strongly Typed view vs Normal View vs Partial View in Asp.NET MVC

I am Newer in ASP.NET MVC. I can not Clearly Understand the difference among
Strongly Typed view vs Normal View vs Partial View vs Dynamic-type View
in Asp.NET MVC. Anyone describe me about this terms.
Thanks in advance!!!
Strongly Typed view
A view which is bound to a view model. For example if you have the following view model:
public class MyViewModel
{
public string SomeProperty { get; set; }
}
that is passed to the view by the controller action:
public ActionResult Index()
{
var model = new MyViewModel();
model.SomeProperty = "some property value";
return View(model);
}
the strongly typed view will have the #model directive at the top pointing to this view model:
#model MyViewModel
...
<div>#Model.SomeProperty</div>
Partial View
The difference between a view and a partial view is that a partial view only contains some small HTML fragments that can be reused in multiple parts of a normal view. For example you could define the following partial view:
#model AddressViewModel
<div>Street: #Model.Street</div>
<div>Country: #Model.Country</div>
and then render this partial view at multiple places in your main view to avoid repetition of the same code over and over:
#model MainViewModel
...
<h3>Personal address</h3>
<div>#Html.Partial("_Address.cshtml", Model.PersonalAddress)</div>
...
<h3>Business address</h3>
<div>#Html.Partial("_Address.cshtml", Model.BusinessAddress)</div>
Dynamic-type View
A view which doesn't have a model or one that uses weakly typed structures such as ViewBag. For example you could have a controller action which sets some property in the ViewBag:
public ActionResult Index()
{
ViewBag["SomeProperty"] = "some property value";
return View();
}
and the corresponding view you could access this property by using the same key in the ViewBag:
<div>#ViewBag["SomeProperty"]</div>

Asp.Net MVC 5 calling PartialView with a different Model

I want to call a PartialView from my Index.cshtml which uses different Model that I want to call PartialView.
My Index.cshtml
#model IEnumerable<myappname.Models.Post>
...
#Html.Partial("_Block")
_Block.cshtml
#model myappname.Models.RightBlock
<img src="#Model.blockContent" width="330" />
Controller.cs
...
public PartialViewResult _Block()
{
int id = 2;
RightBlock rb0 = db.RightBlocks.Find(id);
return PartialView(rb0);
}
...
Please ignore the id because I just want to call it statically, not dynamically.
When I run the Index Page, I get an error:
The model item passed into the dictionary is of type
'System.Data.Entity.Infrastructure.DbQuery`1[myappname.Models.Post]',
but this dictionary requires a model item of type
'myappname.Models.RightBlock'.
How can I pass different model to call PartialView?
Thank you.
Use Html.Action
Index.cshtml
#Html.Action("Block")
Controller.cs
public ActionResult Block()
{
// Your code
return PartialView("_Block.cshtml");
}

How to Convert an IEnumerable model object to simple model object in view , MVC3

I have to pass a model object to my create view , so that on submit click this model object gets updated.
But at the same time iam using a list of data in its main Layout for rendering a dynamic menu.
Both use the same model class. But for one i give as
#model IEnumerable<DynaPortalMVC.Models.Page>
and in the create view i give as
#model DynaPortalMVC.Models.Page
In Controller:-
public ActionResult Create()
{
return View();
}
But this gives me an error in _Layout.cshtml, hence the above actionresult is not even executed.
Exception Details: System.ArgumentNullException: Value cannot be null.Parameter name: source
So i think i should pass the Ienumerable object to create view and then convert it there to simple model object, so that it can be updated. Please advice on this.
It's questionable whether you should have #model directive in your Layout. In my opinion you should render your dynamic menu by using a partial view and an action in your controller invoked from Layout by calling HtmlHelper.Action method, something like this:
Partial view for the menu (file named _Menu.cshtml):
#model IEnumerable<DynaPortalMVC.Models.Page>
<ul>
#foreach (var page in Model)
{
<li>#page.Name</li>
}
</ul>
Action named _Menu in your Home controller (or any other):
public ActionResult _Menu()
{
IEnumerable<DynaPortalMVC.Models.Page> model = new List<DynaPortalMVC.Models.Page>
{
new DynaPortalMVC.Models.Page { Name = "Page1" },
new DynaPortalMVC.Models.Page { Name = "Page2" },
new DynaPortalMVC.Models.Page { Name = "Page3" },
new DynaPortalMVC.Models.Page { Name = "Page4" },
};
return PartialView("_Menu", model);
}
This line of code in your Layout to render a menu:
#Html.Action("_Menu", "Home")

set value for a textbox in partial view razor mvc

I have following scenario:
My Index page, uses a layout which has a partial View ebbeded in it. the partial view contains a search text box.
For a particular scenario, i need to set the text of the search box with my viewdata[] for index page.
is it somehow poosiblein mvc3, asp.net 2010 to set the value of textbox in partial view from the viewpage?
You could make your partial strongly typed to some view model:
#model SearchViewModel
#using (Html.BeginForm())
{
#Html.LabelFor(x => x.Keywords)
#Html.EditorFor(x => x.Keywords)
<button type="submit">OK</button>
}
and then when inserting the partial you could pass this view model:
#Html.Partial("_Search", new SearchViewModel { Keywords = "some initial value" })
or even better the view model of your main view will already have a property of type SearchViewModel and you will be able to call the partial like this:
#Html.Partial("_Search", Model.Search)
Now obviously in your Index action you no longer need to use any ViewData, but you could directly work with your strongly typed view model:
public ActionResult Index()
{
var model = new MyViewModel
{
Search = new SearchViewModel
{
Keywords = "some initial value"
}
};
return View(model);
}
You can always make the partial view strongly typed (even if the model is just a string) and pass the value you need.
public class MyModel
{
public int ValueForView {get;set;}
public string TextBoxValue {get;set;}
}
-Index.cshtml
#model MyModel
#{ Html.RenderPartial("PartialView", Model.TextBoxValue); }
-PartialView.cshtml
#model string
#Html.TextBoxFor(m => Model)
As I understand your issue, the partial view is in your layout and you need to get data into it.
In this case layouts are processed last but passing data to it your options are somewhat limited. You can use an ActinFilter or ViewData.
ViewData is the easiest, and also the messiest so I don't recommend it.
ActionFilters would work, but you could just process your partial by simply calling in your layout:
#Html.RenderAction("PartialViewAction", "PartialViewController")
Unless I'm missing something I don't believe the other answers addressed that this is in a layout, hence a different issue.

Categories