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.
Related
Firstly, I'm beginner of the MVC.
I have a controller named FinanceController. I have two views named StoreEvaluationForm and SendStoreEvaluationToPdf of this control. I want to call a first view's public static function, which is defined in #functions, from second view.
I guess views have hidden class in mvc. Because I realized when I mouse over first view's function, VS shows its class named _Page_Views_Finance_StoreEvaluationForm_cshtml and its namespace named ASP. However I couldn't find any way to accessing another view's function. In second view ASP namespace has only its class named _Page_Views_Finance_SendStoreEvaluationToPdf_cshtml.
To be clear, function in the view is a C# function not a javascript function. Its definition is:
#functions
{
public static string NumberFormatter(double? number, bool percent = false)
{
return number == null ? null : string.Format("{0}{1}", number.Value.ToString("N2"), percent ? "%" : null);
}
}
Yes, this can be achieved. I tried it with the default MVC project in Visual Studio 2015.
Content of About view:
#{
ViewBag.Title = "About";
}
#functions
{
public static string DoStuff()
{
return "Content from About view.";
}
}
Content of Index view:
#{
ViewBag.Title = "Home Page";
}
#_Page_Views_Home_About_cshtml.DoStuff()
... (other stuff)
In Index view there is a red squiggly line below _Page_Views_Home_About_cshtml displaying :
The name _Page_Views_Home_About_cshtml does not exist in the current context
Despite the error message, the application builds successfully and I can see the message from the About view when navigating to /Home/Index.
I have a view that has a string variable and an editorfor. I'd like to send the string variable to the editorFor template partial view
//parent view
string message = "message";
#Html.EditorFor(m => m.someObject)
//editor template
#model someObject
var message = messageFromParentView;
<div>#message</div>
//other inputs for someObject
how do I go about doing this?
You could use ViewBag to do this. Set property on parent view or controller and access it inside editor template code.
Parent View
#{
ViewBag.Message= "message";
}
Editor Template
<div>#ViewBag.Message</div>
I have an action defined like this:
public ActionResult TempOutput(string model)
{
return View(model);
}
And also, I have its view defined like this:
#model String
#{
ViewBag.Title = "TempOutput";
}
<h2>TempOutput</h2>
<p>#Model</p>
Then, at one place, I have a return statement like this:
return RedirectToAction("TempOutput", "SEO", new { model = "Tester text" });
And the point is that when I get to my TempOutput view I get an error message saying "The view 'Tester text' or its master was not found or no view engine supports the searched locations.". But I jsut want to print the value of the string inside my view. How can I achieve it?
You are calling different override of View than you want:
View(string viewName);
You want to call View(string viewName, string masterName, object model) like following:
return View(null, null, model);
You can also specify explicit value (i.e. "TempOutput") for view name.
Alternatively you can force selecting View(object model) override by casting "model" to object:
return View((object)model);
Or, you can overload it using Named Arguments
return View(model: model)
If I remember correctly if you have to use the RedirectToAction you can pass model data like this:
TempData["model"] = "Tester text";
return RedirectToAction("TempOutput", "SEO");
More information about TempData found here.
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");
}
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.