MVC :: @Model Not Supported In Razor Views For VB?
Feb 27, 2011Is not the @Model MyModelClass notation not supported in VB Razor Views? It's not working for me. What is the construct for it?
View 2 RepliesIs not the @Model MyModelClass notation not supported in VB Razor Views? It's not working for me. What is the construct for it?
View 2 RepliesI've recently switched over to using the Razor view engine, and I want to specify a namespace to use in a view. I've tried adding an entry to the Web.config file, ie:
[Code]....
But none of my views recognise any of the classes inside that namespace, so I have to declare the namespace in every view that needs it. why the Web.config approach isn't working?
I am trying to access razor file, like a templating file, directly from the browser- without an action.
I have created a new folder - test - and put there a index.cshtml file.Put also an web.config and pasted bits from original web.config
The error says :
Unable to cast object of type 'ASP._Page_help_Views_Home_Index_cshtml' to type 'System.Web.IHttpHandler'
I would like to do some customizing to the T4 template used to generate code for views added using the Add View dialogue in VS 2010. Assuming this is, in fact, done with T4 like traditionally ASPX views, where can I find this template? I have successfully customised the ASPX template, but I can't recall where I found it.
View 2 RepliesUsing the Razor engine how do I create Partial Views. Do I create a normal View and calling the PartialView() in the controller will only output the body content and when calling View() in the controller will output the full page with layout. If not, do I need to create specific Partial Views and then use RenderPartial in the View.
View 6 RepliesWhy are Razor views not generated as partial classes? Making them partial classes gives us the option to have a code behind where we can do some of the stuff we're forced to do in the template itself in the code behind file.
Yes, I know the standard "go-to" thing is Html helper but I don't want to use Html helpers for these things as they are too specific to the view in question and besides, having a clutter of extension methods is just not the way to go.
I'd like to see Razor views generated as partial classes.
Im currently using a method that looks like the following code to add script and css files to the head of the Layout file.
[Code]....
My problem is that this is a static method meaning it persists the list of stylesheets and script files.
I need to do the same thing that this does but without keeping it persistant.
I need the lists to be remade on every request since they change from page to page what the look are on that specific page.
Is it possible to clear the lists on every request prior to adding the scripts that are needed or maybe after they have been rendered out?
Update:
The reason for not using a section, RenderPartial or RenderaActions is to prevent the same stylesheet or script file to be added more than once to the Layout file.
The site im building has a Layout_.cshtml with the basic layout. This in turn is used by a View that loops through a list of items and for each item a RenderAction is called that outputs the specific partial view for that item. These partial views sometimes need to add stylesheets and scripts.
As there can be needed to add many different scripts and stylesheets from different partial views a global list for styles and scripts were the only way i thought this could be done so there is a global place to check if a script is allready added to the collection or not and then render them all at once in the order they were added.
Update 2:
The real question is how to do the same kind of function (a global list) but without using a static Extension method.
On my projects I am using Razor View engine and I need to define custom Partial folders:
[Code]....
I had this because there was a problem in MVC 3 Preview. Was it corrected?
How should I register the Partial Views in MVC 3 Beta?
I have 2 partial views and models for them. I want to show both views on page and i assume i need one model to pass to the page. Do i have to create all the properties again for both partial views into one model?
view model 1
[Code]....
view model 2
[Code]....
I am trying to populate a drop down list from my controller for a create action. I can display the dropdown as I want but when I cannot get the value to be stored in the database. My models:
[Code]....
Controller: [Code]....
View: [Code]....
This above snippet will show me the message "The value '50117dae-a7d7-4371-926f-7b1f7aaec133' is invalid."
if I change the line to @Html.DropDownListFor(ct => ct.CollectionType.CollectionTypeId, new SelectList(ViewBag.CollectionTypes as System.Collections.IEnumerable, "CollectionTypeId", "Title"))
@Html.ValidationMessageFor(model => model.CollectionType)
the page will not do anything - I believe this could be because the controller is trying to add a new CollectionType.
When it creates a view using the list scaffold the headings are hard coded. I want to pull the headings from the data model, but no luck. I was assuming LabelFor would be the solution. I must be missing something basic.
View 2 RepliesIn my project I use Strong Types Views. Because I find it nice structured.
public abstract class AbstractViewData: View Page ( ICollection Foo; )
public class HTML Component View Data: AbstractViewData ( string Foo2; )
I have the same structure in my code, as in the corresponding pages.And here starts the problems. I would like to use HTML.Display (o => o.Foo) could be a customer for that matter.But my Strong Types Views have not posted Metadata Model into my classes.Like: Return View ( "FooView", customer); would.Is there a way to write some code that can solve this problem for me?
Note: I'm working with MVC3 RC and Razor views.I've got this model:
[Code]....
I want to have a strongly typed create view for this model, that allows you to insert 1 to many addresses and phone numbers as part of the view. Ideally, the Action is strongly typed as well:
[Code]....
What would this view look like to facilitate creating 1 to many Addresses and PhoneNumbers, as well as having the state re-created correctly if the ModelState fails when posting? I've tried a number of variations including EditorTemplates and can't figure out how to do this.
When i loading modules from different places i injected them to controller factory, but when it is trying to load strongly typed views it can't find model type that is contained in module assembly.I made new ViewEngine and now it looks in right places but it cant load strongly typed views.
So as i understand i need to show the view where to find class model. If you have any other ideas please tel me. And one more i need this problem to be solved for WebFormViewEngine and RazorViewEngine.
I am learning MultiView control.Here are question:I added 5 views in the MultiView but all views are tight together. I can not drag and drop another control such as text boxes or labels into view area.
View 15 RepliesI have an MVC view that contains a number of partial views. These partial views are populated using partial requests so the controller for the view itself doesn't pass any data to them. Is it possible to reload the data in one of those partial views if an action was triggered in another? For example, one partial view has a jqGrid and I want to refresh the data in another partial view when a user selects a new row in this grid. Is there a code example for this scenario (in C#) that I can look at to see what am I doing wrong? I am using ajax calls to trigger a new request but non of the partial views are refreshed so I am not sure if the issue is with the routing, the controller,
View 1 RepliesI'm building an MVC 2 RTM app, and I want to be able to share my model across applications. I'd *like* to be able to implement it like:ASP.NET MVC2 app (holds Views and Controllers)Class library to hold Model(s)WCF app to handle the data transactions with the models via different data stores across apps I had the MVC app working fine, but I wanted to abstract the data stuff and be able to work with the model across apps through the WCF site, so I created a class library project and moved all of the Models classes into that and set-up a WCF app, then added project references to the MVC and WCF apps that point at the class library. The idea was I can create services that take and return objects from the model via method calls across apps. It appears that everything's wired up correctly in the MVC project, so I'm passing the objects stored in the Models class library between controllers and views and everythig is compiling just fine, but for some reason the data is not being passed back from the views to the controller on POST -- all of the properties in the classes are null or empty.
When I debug the app, I can see that the values are stored in the model data dictionary but not the model object itself. What am I doing wrong? Am I on the wrong path, or missing something obvious (to some)?
Is it possible, inside a Custom Model Binder, to fire "something" that "says" the value is invalid so it gets handled by validation part?
Basically, I am getting an exception when the value for the property is invalid.
I made a few changes to the DB in SQL server management studio then right clicked on the .edmx doc to get it to update. That seemed to work fine but when i compiled the app everything that referenced the EF seems to be broken.The Error list now contains the below error for all classes that used it.
The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?)
I have an issue of static variable in an asp.net application. Let's say I have a server with 8 CPUs running windows 2008 R2 hosting an asp.net webbsite, nothing fancy here, no funny configuration.
Is the following statement true? at any single time, there's one and only one process is running and accepting requests, even in the recycling stage. The reason I am asking is: I have a static field in my class, and I want to make that's the only static instance in the website. I've heard in some circumstances, IIS is recylcing your application, and it will start a new process, but the old process is still working, thus I will have 2 static instances in memory, which defeat the purpose of static field.
One step further, let's assume there might be 2 instances in memory, can I assume there are at most 2 instances at any single time? And can I assume once the second instance is up, the first instance will NEVER accept new requests?
Another question: Recently I have a problem with an applicationdomain concept. Looks like if an application domain causes a memory leak, unload the domain will not release the memory (Umanaged leak). So to what extent Application Domain is isolated?
I am trying to make a post that should use the Default Model Binder functionality in ASP.NET MVC 2 but unfortunately I can't get through. When I click on the checkout button I populate a form dinamically using jQuery code and then submit this form to the server. This is the form that get submitted
<form action="/x/Order/Checkout" id="cartForm" method="post">
<input name="__RequestVerificationToken" type="hidden" value="UDjN9RdWheKyWK5Q71MvXAbbDNel6buJd5Pamp/jx39InuyYIQVptcEubIA2W8DMUzWwnZjSGkLspkmDPbsIxy8EVuLvfCSZJJnl/NrooreouptwM/PaBEz2v6ZjO3I26IKRGZPqLxGGfITYqlf8Ow==">
<input id="CustomerID" name="CustomerID" type="hidden" value="1">
<input id="FirmID" name="FirmID" type="hidden" value="2">
<input type="hidden" name="CartItems[0].ServiceTypeID" value="1">
<input type="hidden" name="CartItems[0].Quantity" value="1">
<input type="hidden" name="CartItems[1].ServiceTypeID" value="2">
<input type="hidden" name="CartItems[1].Quantity" value="1">
</form>
This is the jQuery code that handle the submit event for the form
$("#cartForm").submit(function (event) {
event.preventDefault(); var form = $("#cartForm");
var panel = form.parent(); panel.parent().block();
$.ajax({ type: "post", dataType: "html",
url: '<%: Url.Content("~/Order/Checkout") %>',
async: false, data: form.serialize(),
success: function (response, status, xml) { panel.parent().unblock(); },
error: function (response) { panel.parent().unblock(); } }); });
This is the controller action that should be get called
[HttpPost]
[ValidateAntiForgeryToken]
public virtual ActionResult Checkout( CartModel cart ) {
} And finally this is the CartModel class involved
public class CartModel : BaseModel{
public int CustomerID { get; set; }
public int FirmID { get; set; }
public List<CartItemModel> CartItems { get; set; }
public CartModel() { CartItems = new List<CartItemModel>();
} } public class CartItemModel : BaseModel
{ public int ServiceTypeID { get; set; }
public int Quantity { get; set; } }
But the default Model Binder does not bind the web form data to a CartModel class. Using Fiddler I have been able to see that the data sent to the server is correct as you can see from the following snapshot.
when I use Html.HiddenFor( model => model.OwnerId ) to create a hidden field, the value assigned to that field is zero. When I use <input type="hidden" value="<%: Model.OwnerId %>" /> to add the hidden field to the form, the value is assigned correctly.
Why would Html.HiddenFor( model => model.OwnerId ) not get the correct value from the Model object? Am I supposed to load model state somehow separate from returning the model object from the action method? Here is the view:
[Code]....
The Create action method is relatively straight forward.
public ActionResult Create( ... )
{
ViewStockItem item = new ViewStockItem();
item.ActionCode = ActionCode.Add;
if (item.OwnerId == 0)
item.OwnerId = 7;
BookOwner owner = db.BookOwners.Single(c => c.OwnerId == item.OwnerId);
item.OwnerName = owner.OwnerName;
return View(item);
}
I've been used to decorating data model classes with data annotation attributes, but the purist in me baulks slightly at including purely presentational attributes such as display format here. I am, however, quite happy to keep validation centric attributes here. One good reason I have to continue keeping all annotations etc. in the data model is that my view model aggregates data model classes, e.g.
my ViewModelBase.DetailItem<TEntity> property in the view model is just a reference to an entity class in my data model. If I wanted to move presentational annotations to the view model, I would have to quite radically revise my design to one where I duplicate data model properties in my view model and use an object mapping tool to populate view model objects based on data model objects.
[code]....
Say I got a domain model as follows: (and my repository expect an instance of this object)
[Code]....
And a view model (which my views are based on)
[Code]....
At the moment I got it like this and have my controller action manually create a new Person object from the PersonModel object before passing it on to the repository, which does not feel right.
So I tried to have PersonModel inherit from Person and pass that to the repository (also tried casting the PersonModel to a Person object first), but that don't work out.
What's the right way to have PersonModel automatically cast to Person? I want to keep this logic as my current structure allow me to keep things really loosely coupled, with the repository layer not having a clue how it's being used.
i m trying to save image to server by giving path,but error comes
"URI formats are not supported."