Generate A Path - Url From A Route Within A Model Class?
Nov 26, 2010
How to generate a path/url from a Route in the Routes table? only this time I'd like to be able to build a url within one of my Model (partial) classes. I'm defining a new property that will contain the text to be rendered within an rss feed, and want to inser urls (within anchor tags) in this text. I found the UrlHelper.GenerateUrl method, but get unstuck once I get beyond passing in the appropriate RouteName, ActionName and ControllerName.
I have an ASP.NET MVC web application, and I've registered a number of routes in my Global.asax.
I would like to know how I can programmatically build (generate a string url) any one of those registered routes from within my Controller.
I did the same thing in Web Forms with .NET 4.0 using Page.GetRouteUrl(routeName, routeParams) but can't figure out how to do the same thing in MVC (I'm an MVC newbie).
So, if we have in route defined parameter with same name as property at model and then we call it from strongly typed HTML helper (for example Html.TexBoxFor(x => x.PropertyName)), we get value from route parameter instead of property of model.
For example.
We have defined follow route:
[Code]....
Very simple model:
[Code]....
Next action in controller:
[Code]....
And very simple view:
[Code]....
Result you can see at follow picture:
I think what this dirty example described the essence of the problem. Let me know if I'm wrong.
I'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)?
I have an application that I am trying to add url routing to. Part of my application though, looks at the requested page, and looks for a folder that the file requested is in, and applies different rules depending on the folder. For example, the two files
~/Folder1/page.aspx
and
~/Folder2/page.aspx
would have some different rules applied to them because they are in different folders.What I need to do, is get the actual file information from a request when that request comes from a routed url. I do not need the entire file path, just the relative path. Essentially, I am looking to retrieve the value of the third argument in the MapPageRoute method, e.g.
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
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.
I'm trying to make an application that can host multiple ecommerce front ends, so I need to figure out how to route all requests to one class which then parses templates and handles all output.
So my question is then how do I route all requests to one class?
In PHP I would use mod_rewrite and have all requests go to index.php and add "main = new MainClass();", but I have no idea on how to achieve this with ASP.NET
The MVC framework isn't what I'm looking for, since I will host several domains with one system.
Out of good design practice and organization, in which project should a local development SQL database be in a web based application? I'm just starting my assignment so I currently have 'CompanyName.Web.UI' and 'CompanyName.Domain' projects setup. Im using LinqToSql and creating my database from POCOS, meaning I'll have to create some code to generate the DB from my domain model. I.e.
DataContext dc = new DataContext(connString); dc.GetTable<TableType>(); dc.CreateDatabase();
I use Linq to Sql (although this is equally applicable in Entity Framework) for my models, and I'm finding myself creating buddy classes for my models all the time. I find this time consuming and repetitive. Is there an easy way to automatically generate these buddy classes based on the models? Perhaps a visual studio macro?
[MetadataType(typeof(PersonMetadata))] public partial class Person { } public class PersonMetadata { public object Id { get; set; }
it is possible to customize the typed view generation process to generate the proper controls based on the fields annotations of a model. For Example I have a Person Model that contains Foreign Keys like CityID,DepartmentID etc. So When i generate a view againt the Person Model it creates textbox's for all the Person fields but i want dropdownlists against FK fields. It Should be smart enough to at least geereate the peroper controls based on the Person's field annotations. How can i acheive that? Has anybody worked on that?
I have a generic class and I want to generate a URL when given the controller and action values. The most obvious method is Html.Action(controller, action), but this can only be called within a View. I tried adding 'using System.Web.Mvc;' to my class, but this doesn't give me the Action() method. I also tried converting this to an HtmlHelper class, but again no luck.
How do I generate a fully qualified URL within a class using the controller and action names?
I am trying to generate a random number from within a class and am getting "Type 'Random' is not defined" - does anyone have any ideas. Here is the code
**note the code works on an aspx.vb page, but not from within my class Dim RandomClass As New Random() Dim RandomNumber As Integer RandomNumber = RandomClass.Next(3)
I'm trying to write a txt file from a vb class (.vb) and need to know where my app is locates, most users upload my proyect in a virtual directory from iis and I cannot use the Application.Path property because I'm not doing this from a form.
I have some questions in my mind after went through the video tutorials.
Is Model a entity class type? encapulated attribute and methods (business logic). because i see in videos , people use LINQ to generate models which can only use LINQ to modify/insert/delete data.
Is it a good practice to generate Model using LINQ?
I'm reading a bit of Pro ASP.NET MVC Framework and following a few tutorials written in the book. In the very first tutorial, creating a MVC site for dinner (PartyInvites), I encoutered this problem. While I try to reference some properties from Model class, the compilation error occured. Eg. the following code failed.
[Code]....
and the error message
Compiler Error Message: CS1061: 'object' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) I imported the Model folder into the controller too. I dunno what I'm missing here. Sorry, if it sounds a bit umm hard to understand cuz I just started picking up MVC this morning. Looking for any input.
I have need to provide a URL to a controller and action in one of my model classes. Is this possible to do with out providing the entire webaddress as well?
I have just added routing in a new asp.net 4 web forms application, and have got my route table set up ok and page requests are working fine. What I now want to do is use the Page.GetRouteUrl method whenever I need to generate one of my seo friendly Url's. I started to implement this across various pages then thought it might cut down on code a bit if I had a method in one of my own helper classes, that constructs this url (using the Page.GetRouteUrl method) as I might have several parameters that need to be specified against the Page.GetRouteUrl method each time.
However, within my helper class, it doesn't know what 'Page' is. I was thinking I could pass in 'Page' from the page that wants a routed url to be generated, but in some cases I want to construct one of these Url's in another class which doesn't know what 'Page' is, and in that scenario wouldn't be able to pass it in as a param (and therefore wouldn't be able to use the Page.GetRouteUrl within my helper class). What is the normal approach when wanting to use Page.GetRouteUrl within classes that don't have an instance of the Page object?
I would like to generate my links automatically via a static class in each of my aspx pages (or in a common BasePage).Currently I use this:
private const string TEMPLATE = "~/One.aspx"; public static string Link () ( string link = String.Format(TEMPLATE); return link; )
But the name of my page, One.aspx is hardcoded. Is it possible to generate the path instead of this hardcoded constant TEMPLATE. You should know that I do not instantiate the class before creating the link.
I have created an extended method for DataTable. I want to generate a class automatically in my solution when i call that method while writing a code as Visual studio does when it doesnt find the method.e.g mytable.mymethod(); Generate a class
i wanna use to subsonic 3.0.4 but i dont know, how can i start to generate class and use to new Template of T4. example: create new solution for generating to table's class and add to my project. ??