I haven't done web applications before, but I'm familiar with C#, and am trying to create a website using MVC3.
I have a page with an HTML form, i have it set to use GET method, and on submit to go to a new page.
The new page will load fine, and have the parameters in the URL like this:
http://localhost:55751/Home/ClassAdded?Semester_Fall=Summer&Ticket=123&Year=2012
On this new page, how do I get the parameters from the URL to work with them in C#? Essentially, I want to just take those params and put them in in a database.
Have a look at http://www.asp.net/mvc/pluralsight for a nice little introduction.
Easiest way would be to define your model and have a strongly typed view that will send an instance of the model to the controller. There you would (very easily) just store the model instance received to the database (use Entity Framework or a Linq Data Context for that, up to date people would use EF though).
There is no real magic, just get to know the MVC pattern and then how it is implemented in MVC 3 ...
ps: i don't think the question would earn a -1, sure (s)he could have had a go on google... but well shrug
I recomend you to read some book: http://www.amazon.com/Pro-ASP-NET-Framework-Steven-Sanderson/dp/1430210079. You need to use action parameters.
Related
I'm building a web application that consists only of one page (Literally one page, no HTML generation). The whole purpose of this application is to collect data from users and then save it into a SQL Server database.
I want to use Javascript to validate and collect the data from the HTML Form, put it in a JSON object, and then use C# and .NET to insert the data into SQL Server.
I have no clear idea on how to achieve this, and honestly, I'm not really sure if this is a valid model or not!
Any pointers or ideas on what to look for and where to start? And how can I achieve this in the simplest way possible?
I would suggest using jQuery and ASP.NET MVC. Here is an example:
Posting simple json to MVC action controller method with jquery
or this one:
Send data to MVC controller using JSON
We are currently learning how to use c# at university, and we have been given a project that uses mvc, I would like to know what mvc is all about and how do we implement it? Any suggestions will be appreciated
No short sweet answer here will give it full justice, please have a look at this article.
wiki Model View Controller
or maybe even this
https://softwareengineering.stackexchange.com/questions/127624/what-is-mvc-really
As for the implementation of the pattern it will depend entirely on what your using to program with. I would advise search for mvc in the language or technology of your choice and follow a few examples, then get started once you understand the concepts of MVC
Have a look at this article from CodeProject.
It explains the MVC using a simple C# WinForms application.
http://www.codeproject.com/Articles/383153/The-Model-View-Controller-MVC-Pattern-with-Csharp
This is the best site to go. http://www.asp.net/mvc
For me if you want full control and testable web app without having to worry on the UI, I would use MVC. Everyone is going to this route. And so am I. =)
MVC firstly stands for Model View Controller.
Controllers
Controllers are nothing but classes which inherit from Controller base class and have different functions.
For example:
public class SchoolController : Controller
{
public SomeReturnType AllStudents
{
// Return all students
}
// More Functions here...
}
And these functions are responsible for providing response to the client request. When a client tries to open a URL (www.example.com/School/AllStudents) -
MVC does mapping to Controller Class SchoolController and then to the method AllStudents
Models
Now models are again classes. Models represent your database tables. So for example, if you have a student table in SQL then we will have Student Model in MVC. Example:
public Class Student
{
public int StudentId{get;set;}
public string Name {get;set;}
public DateTime DOB{get;set;}
}
Now in order to bring data from the database and put into our model, we use EntityFramework. So entity framework connects to the database and puts the data from the database table into our model.
Views
Now understand as I said earlier controller action will get called on client request. Now It is the duty of controller action(function) to provide response to the client requests. Now When the client requests for School/AllStudents, I can return it a simple list of strings, and it will get displayed on browser in the raw form. i.e. No formatting just the plain data. Now obviously we would not do that, instead I will make a good HTML page with the List of students and then send to the client.
So here comes Views in the picture. Views are simply HTML templates. In which you fill your data and return to the client.
In my mind I think of MVC as a routing mechanism.
To proceed further on learning MVC i would advise you to brush up your basic concepts on:
LINQ
Lambda Functions
Extension methods
Anonymous Types
These concepts are used everywhere in MVC.
Good Luck!
I'm making a small Forum where user can start topics and reply, it's just like bulletin board, I'm doing this asp.net project for learning purpose, as I noticed in many forums or bulletin board they use separate pages, (actually I don't know how they achieve) to display result
for eg: How to read an external html page using jquery?
in that for every query " questions/6327018/how-to-read-an-external-html-page-using-jquery" this will be changed automatically based on the query, I don't know how to achieve this in asp.net.
if user select his topic, based on topic it has to show in separate page like above the stackoverflow eg ..
Stack overflow is written using MVC. Hence in the case of this posting you have a controller named "Questions" (the class is really named QuestionsController) that takes a parameter of "6333181" as an integer. Your route points to some method - generally named "Index". So in QuestionsController.cs you would have a method like:
public ActionResult Index(int postingId)
Note - the URL for this posting can be
How to display query results in seperate page like Stack Overflow in asp.net without anything else.
In MVC this is quite simple and is designed this way out of the box. No url rewriting required, this is how you setup your routes in the global.asax.cs in an ASP.Net MVC application.
For stack overflow this can be confirmed here:
https://blog.stackoverflow.com/2008/09/what-was-stack-overflow-built-with/
The way that is achieved is using URL rewriting. Behind the scenes that url "questions/6327018/how-to-read-an-external-html-page-using-jquery" is redirected to something presumably like "questions.aspx?id=6327018".
That's the first step, you need to get your application to handle URL rewriting. It's pretty easy these days in IIS/ASP.NET, there's a built in extension so you can just set that up in your web.config.
The second step is using that id=[number] and querying the database for the data associated with the question with that id and displaying it on page.
I have a form where users can enter data that ultimately would be used to create new Customer, CreditCard, and Membership objects. I want to add recaptcha to the page. This does it's validation on the server side. All other verification will be done client side with javascript with perhaps an extra layer of validation on the server side.
Should the captcha fail, I want to redirect the user back to the form and reenter their information automatically, and do some pretty jquery to highlight the invalid field.
I'm looking for the best practice to do this. Should I create some sort of display object that has a Customer obj property, a CreditCard obj property, and a Membership obj property, or should I just pass the MVC FormCollection object back to the page and use it to populate the form?
While the custom display object would be more work, I plan on using ViewData to store a reference to the field that failed validation. Therefore the custom display object would not require me to use the ViewData dictionary, which I like to avoid.
What are your thoughts on what I should do, weighing best practices as a factor?
I think working with strongly-typed objects is better then just using the FormCollection.
I suggest you create a ViewNodel object that will have all the property's you need in the view, and work with a strongly-typed view too.
ASP.NET MVC Partial Views and Strongly Typed Custom ViewModels
As CD mentioned above, a ViewModel that wraps (and conditionally exposes or augments, as needed) the objects you're working with on a page is fine for display, just be a bit more selective in what you post back.
As far as error handling goes, you'll probably want to look into using ModelState as a generic way to cleanly deal with and present them.
I have a data input module where I add the information of my product and its sub information like:
product basic info
product price info
product price details
price info and price details are related to product and are lists
In my web forms approach I would store my main product object on the view state and I would populate it's pricing info and details while doing ajax postbacks. This way I can create a compact module that is very user friendly in terms of defining a lot of data from one place without the need to enter these data from seperate modules. And when I am done I would do one product.save() and that would persist all the data to the respective tables on db.
Now I am building similar app on .net mvc framework and pondering on what would be the good way of handling this on mvc.
I don't resonate towards storing all this on client side till I click save. And saving to the db after each action makes me remember the days I was coding on asp.
Will appreciate your inputs on ways to approach this on mvc framework
I believe the best way of doing this is storing the data on the client side. It reduces unnecessary postbacks and improves responsiveness of your application. If you really want to store it on the server, you can use SessionState.
If you really want to store it in something like ViewState, you can go with a solution like this: ASP.NET MVC - Is there a way to simulate a ViewState?. However, I recommend against it as it will make things more complicated. Doing it client-side is probably the most elegant way and storing it in SessionState is the easiest.
Remember that you can always escape the MVC pattern and use a simple Web form for that specific page (which will give you ViewState where you need it): ASP.NET MVC controller actions design
store your Product list to the Model of the view and each time you change a value you can do a Ajax post to the controller and save the changes to the db, use partial views to display each item in your product list
you can try to integrate http://www.castleproject.org/ActiveRecord/ for easy saving and updating. That way you can just map your Model on your database using ORM(Object Relational Mapping). It takes a bit more work in the beginning but you will end up with simple commands like product.Update() and product.Create()