|
Well you can use a Razor Html Helper
ActionLink to make a button
@Html.ActionLink("Create User", "Create", "User", new { userID = "-1"] }, new { @class = "btn btn-default" })
Or an A element using Url.Action for the href, then change the class to make it look like an button.
<a class="btn btn-default" href="@Url.Action("Create", "User", new { userID = "-1" })" data-toggle="tooltip" data-placement="bottom" title="Create User" data-original-title="Create User">Create User</a>
I did look at your code. You need to create a model in the controller actionresult "Create" and return it in the view, so the page has a model to work with.
|
|
|
|
|
I am new to ASP.Net MVC, do I need to create all those html helper classes, it is very confusing for me now I am used to that drag and drop in Web Forms design. Now it seems I need to learn lots of lots of things.
Apart from Html Helper classes can you please name me some challenges I would get while I am new and implementing ASP.Net MVC. I am learner I can learn but the thing is if learning is helping me then I can. I tried what could be the reason for the errors that I am getting at these helper classes and model, ViewBag keywords but didn't get much help and reason for that.
And just asking one more thing why is the model and ViewBag are also showing errors as model and ViewBag are not recognizable objects. How can I resolve that issue? Why is that issue coming I am not sure at all.
Please help me I am learning ASP.Net MVC and implementing it.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
modified 15-Jun-16 14:58pm.
|
|
|
|
|
If your model is not showing up in intellisense, then you need to reference that model in the view
So let's pretend this is a view file. I created my Model in a class with the namespace Indigo.Entities.Models so in the first line in the view, I add ...
@using Indigo.Entities.Model
In the 2nd line, I make a reference to the Model
@model model_customer_Addresses
So now it looks like this
@using Indigo.Entities.Models;
@model model_customer_Addresses
@{
ViewBag.Title = @Model.Name + "'s Addresses";
Layout = "~/Views/Shared/Admin/_adminLayout.cshtml";
}
So what is the model? Think of it like a custom container or lunch box. In the lunch box you declare a sandwich, fruit and beverage. You build or declare the model in the controller, then pass the model to the view. The view knows that the model says you have to have all 3 in the lunchbox, and will not let you post the page until all 3 are selected; Validate! once validation is complete, the page will post the model back to the controller which can then pass the model to the database to write a record.
On the form, you create 3 elements, 1 assigned to sandwich, 1 to fruit, 1 to beverage.
Wrap it in a form element, add a submit button, some validation and your done!
As far as Html helpers goes, they are Razor, which creates shortcuts for writing HTML Elements. In other words, all they do is generate HTML. The benefits to using Razor is organizing the
Text, Action and Controller. and it also aids in Url routing. The rendering engine will figure out how to construct or code the URL, and the controller will figure out how to read or decode the Url.
My first MVC project was so hard to learn. I had no clue what I was doing and wondered it it was worth it. Now I'm on my 3rd project and it's well worth it. I'm not going back to web forms.
But be careful using Razor. I'm using less and less of it now.
Consider this, you can use razor to write an ActionLink, or just write an A element
<a class="btn btn-primary" href="@Url.Action("AddressesBilling", "Admin", new { caID = Model.CustomerID, baID = address.BillingID })" data-toggle="tooltip" data-placement="bottom" title="Edit the customers billing address" data-original-title="Edit the customers billing address">Edit</a>
This creates a button using the A element or hyperlink, but I was still able to use Razor to calculate the Url that has routing attached to it. The bootstrap turns the hyperlink into a button.
indian143 wrote: I am new to ASP.Net MVC, do I need to create all those html helper classes, it is very confusing for me now I am used to that drag and drop in Web Forms design. Now it seems I need to learn lots of lots of things.
Not really, just write great HTML and then fill in the spaces using Razor. Razor is tricky, like the @ char rules. Once you declare @, you don't have to use it again in the same line. Just watch the Yellow markers that will help you.
indian143 wrote: And just asking one more thing why is the model and ViewBag are also showing errors as model and ViewBag are not recognizable objects. How can I resolve that issue? Why is that issue coming I am not sure at all.
I don't use the ViewBag, I make better models that include everything I need. The hardest part of MVC is designing the models. Models have to be reusable, compact, not duplicated, organized which is very important.
You have to change the way you think about organizing data. And constantly update your models to be more reusable until your really good at it.
Like take a customer for instance. The customer will have an account name, email address, password, date. but perhaps the customer will have an address as well. So you create a model called address or something.
your model would be
name
email
password
date
address as model_address below
and the address model would be
address1
city
state
country
stateList - List of states for dropdown
countryList - List of countries for dropdown
Hope that helps!
|
|
|
|
|
Yes helps me a lot for taking time to help me and take my ASP.Net MVC phobia out
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
Still it is saying me the same thing that model doesn't exist in the current context and same with ViewBag? Am I missing any references or any Html Helper Classes etc?
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
Did you return a model to the view?
Remember I said you need to update the controller for the view, by creating the model and returning it to the view.
return View(model);
I don't know about the viewbag, I don't use them anymore.
|
|
|
|
|
Yes I am passing the model still it gives me error saying "Name model doesn't exist in current context" at the place where I am mentioning @model
Here is my View
@*@using MVCWithWebApiApp.Models
@model MVCWithWebApiApp.Models.User
@{
ViewBag.Title = "Index";
}*@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Users Details</title>
<script src="~/Scripts/jquery-2.2.4.min.js"></script>
<script src="~/Scripts/angular.min.js"></script>
<script src="~/Scripts/myApp.js"></script>
</head>
<body>
<h2>Index</h2>
<div data-ng-app="myApp" data-ng-controller="userController">
<table style="border:none 0px gray;">
<tr>
<td>
<table style="border:solid 1px gray;">
<tr>
<td style="border:solid 1px gray;">User Id</td>
<td style="border:solid 1px gray;">User Name</td><br />
</tr>
<tr data-ng-repeat="usr in users">
<td>{{usr.UserId}}</td>
<td>{{usr.UserName}}</td><br />
</tr>
</table>
<pre>
</td>
<td>
<a class="btn btn-primary" href="@Url.Action("AddressesBilling", "Admin", new { caID = Model.CustomerID, baID = address.BillingID })" data-toggle="tooltip" data-placement="bottom" title="Edit the customers billing address" data-original-title="Edit the customers billing address">Edit</a>
</td>
</tr>
</table>
</body>
</html>
And my controller I have added this code still it doesn't help me.
public ActionResult CreateUser()
{
User user = new User();
user.UserId = 1;
user.UserName = "Abdul";
return View(user);
}
Am I making any error in my Web.Config here I am putting it here, any suggestion helps me
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
</namespaces>
</pages><br />
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
<add key="webpages:Version" value="2.0.0.0" />
</appSettings>
<system.web>
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
<pre>
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
<system.webserver>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
Yes I am doing it here is the code that I am doing, but still it gives me errors in all those places of @Url, @model and ViewBag all of them, I know you are not using ViewBag but I giving that information because the errors that maybe related anyways I am getting tired with ASP.Net MVC specially the ones that are given Microsoft that are like Html Helper classes etc.
I never faced difficulty in resolving errors in any other place.
public ActionResult Index()
{
var dbContext = new MVCDBContext();
List<User> listOfUsers;
listOfUsers = dbContext.Users.ToList();
return View(listOfUsers);
}
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
Oh your using angular. I thought you were using Razor and HTML Helpers. My Bad it was in your example in the first post.
How to use AngularJS in ASP.NET MVC and Entity Framework - Evangelism - Infragistics.com Blog[^]
With Angular, you make calls to JsonResults, which is like a webservice, only your calling the controller for just json data. Then the Angular takes the Json data and populates the fields. So modify that controller code to return JsonResult, get your data as a list and return it.
|
|
|
|
|
I don't know much difference in both buddy, just asking OK if I am using angular, in the browser should we call the action method or should we call the plain html file? I am confused which to use when?
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
I've never done AngularJS. It's on my list but not today.
Is using Angular a requirement for a job your working on?
|
|
|
|
|
There is an application with us named "MirrorTools" which was existing more than 7 years and we are not the developers (no clue on whether this application developed internally within organization or online product)
We have an Mirrortools.exe and MirrorTools.exe.config file.I am calling this Mirrortools by via an .bat file which carries the following code
@echo off
title TestRaw Refresh
MirrorTools --refresh --dtimeout=86400 A.xml BLog
MailSender --to=aaa@yahoo.com,bbb@yahoo.com --subject="[MirrorTools] TestRAWSubject Refresh USPLSVULM132" BLog
del BLog
and the config file contains following code...
<?xml version="1.0"?>
<configuration>
<system.data>
<DbProviderFactories>
<remove invariant="Oracle.DataAccess.Client" />
<add name="Oracle .NET Data Provider" description="Oracle .NET Data Provider" invariant="Oracle.DataAccess.Client" type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</DbProviderFactories>
</system.data>
</configuration>
While I am executing the bat file i am getting the following error
Unhandled exception : system.configuration.configurationerrorexception: Failed to find or load the registered .net framework data provider.
System.Data.Common.DbProviderFactories.GetFactory(DataRow providerRow)
at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariant
Name)
at MirrorTools.TaskManager.CreateDataSource()
at MirrorTools.TaskManager.RunMirrorJobs()
at MirrorTools.TaskManager.Execute()
at MirrorTools.TaskManager.Execute(Configuration config)
at MirrorTools.Program.Main(String[] args)
Looking forward your help pls
modified 14-Jun-16 9:59am.
|
|
|
|
|
Make sure that you have installed ODP.NET in your system.
Once installed install Oracle.DataAccess.* into GAC using GacUtil
|
|
|
|
|
Overview
The purpose of the application is to allow end-users to quickly enter and manage a list of name/value pairs.
Although this test must be written without any other persons help, any standard reference material that is used during a normal programming cycle may be used (such as online help or books). No third party class libraries or code snippets may be used.
Create a Windows Forms application using a UI similar to the one shown below. The application code should be written in C# , unless you are specifically applying for a position that does not require C# skills or were given instructions to do otherwise.
Required Features:
Name/Value pairs are to be entered into the upper textbox. This textbox will be used by the end-user to quickly add Name/Value pairs to the list below.
When a Name/Value pair is about to be added to the list it must be validated to ensure proper syntax. If the syntax is incorrect then the Name/Value pair must not be added. The Name/Value pair entry format is shown below:
=
Where is the name portion of the pair, and is the value portion of the pair. Only valid Name/Value pairs can be added. Names and Values can contain only alpha-numeric characters. The equal-sign is used to delimit the pair, spaces before and/or after the equal-sign may be entered by the end user (and should be ignored.)
When the ‘Sort by Name’ button is pressed the list will be sorted ascending by Name.
When the ‘Sort by Value’ button is pressed the list will be sorted ascending by Value.
When the ‘Delete’ button is pressed all selected items in the listbox will be deleted.
When the ‘Save as XML’ button is pressed, all of the items in the listbox must be saved to an XML file.
When the ‘Exit’ button is pressed the program will close.
|
|
|
|
|
Sorry, but this site does not provide code to order.
|
|
|
|
|
Member 12579290 wrote: this test must be written without any other persons help
That should be "person's help".
|
|
|
|
|
Member 12579290 wrote: this test must be written without any other persons help
You've failed the test.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
Are you asking us to design and develop the code. ?????????
|
|
|
|
|
Create a Web Forms application using a user interface similar to the one shown below. However feel free to modify the layout to allow for better usability, if desired.
All server-side code should be in C#
Required Features:
1. Name/Value pairs are to be entered into the upper textbox. This textbox is used for adding and filtering Name/Value pairs.
2. When the ‘Add’ button is pressed the Name/Value pair in the textbox is validated, and then if it passes validation it is added to the listbox below. The Name/Value pair entry format is shown below:
3. <name> = <value>
4. Where <name> is the name portion of the pair, and <value> is the value portion of the pair. Only valid Name/Value pairs can be added. Names and Values can contain only alpha-numeric characters. The equal-sign is used to delimit the pair, spaces before and/or after the equal-sign may be entered as padding at the end-users discretion.
5. When the ‘Sort by Name’ button is pressed the list will be sorted ascending by Name.
6. When the ‘Sort by Value’ button is pressed the list will be sorted ascending by Value.
7. When the ‘Delete’ button is pressed all selected items in the listbox will be deleted.
Optional Features
The following features are a test option and may not be required, please refer to the job description to see if it is a requirement
1. When the ‘Filter’ button is pressed, only the Name/Value pairs that match the filter criteria will be shown in the listbox. The search criteria will be taken from the textbox.
Filters have the following format (and are similar to the Name/Value format):
<type> = <value>
Where <type> is either Name or Value, and <value> is a string used for matching against the given type.
2. When the ‘Clear Filter’ button is pressed all of the Name/Value pairs will be shown in the listbox.
|
|
|
|
|
Well what an adventure after 5 days of trying to figure all this out. But after getting my project up and running, I now see the benefits as a whole, and it was well worth it. Many of the earlier questions I had about MVC have now been answered, such as what's up with the Razor, what is Webgrease and how to use it, and how do we minify our CSS and Script files.
So while we were watching Steve Ballmer try to buy the LA Clippers, the new CEO Satya Nadella has been working to let Microsoft developers have more fun creating apps, by unchaining us from the boiler plated project templates and letting us choose how we want to work.
My 5 day journey in the last week has exposed me to all sorts of new concepts that appear to be working great out there such as Node.js, but I digress, and will return back to the topic.
Bower:
So in case you didn't know, Bower is another way to download packages that can be automated. It's not a replacement for NuGet, which is still viable for server side packages, but the preferred way to download front end frameworks and packages such as Bootstrap and JQuery.
Bower compared to NuGet Packages
The NuGet client packages were sort of lame. I used NuGet for Font Awesome, and a couple of other front end packages, and had to go through all sorts of hoops to get them to work. Many of the packages were broken or outdated. Bower allowed me a very easy way to get the latest version of the package up and running in very little time, and did the dependency checking for me as well.
NPM
NPM is sort of like rpm in linux, that runs off Git, which is the git repository. You can download packages by command line such as npm install uglify -save-dev , which will download the package and save it to a package.json file, to use later.
Gulp
Gulp is a JavaScript file that uses the packages downloaded by NPM to customize your project. So with the package uglify, I can write a gulp command to minify my JQuery script files, add the suffix .min and publish them in my custom location.
Gulp can do many things, you can wrap script tag elements in HTML content and use Gulp to read the HTML, bundle those scripts and uglify them. Build customer CSS from Sass sources and minify them. So take for example JQuery 3.0 was released today, I think I'll just change the version number in my package.json and upgrade my project today, for it runs automatically.
Running Build and Clean
Put it all together, bind it to your project's pre-build, post-build and clean events, and you have a very powerful solution that is easy to work on and maintain.
VS2013 - Stripping MVC almost to the bare bones
Yes I had to strip my projects Nuget packages to the bare bones and figure out how to replace the functionality that Microsoft had injected into my project. What a mind bender as to what did those packages did in the first place? Microsoft was quite clever and had the intentions of producing the best possible results for an entry level MVC programmer, but I wanted more infinite control over how my project would be published for production use.
Razor
Well I just got my head out of the sand and realized that I don't have to use Razor. There are many other solutions out there such as Angular.js which actually looks more functional to me. But I have already started with Razor and will try Angular.js in my next project.
Deployment using Docker on Linux or Windows
I had no idea that Docker was out there. Docker is a container that includes a small version of your operating system and your production project. It's not a Virtual Machines, but the concept is similar. So you can develop in your Docker container, and just deploy the container to a cloud service or in house server. This eliminates the headaches that have long plagued us, in which hey it works in development but not in production.
And it doesn't have to be a Windows OS, you can develop on a Linux platform using the new Microsoft ASP.Net for Linux and Mono I believe. Build your Linux platform in Docker, and just deploy that Docker container to a Windows or Linux server running Docker.
Final thoughts
I'm really excited about this today. Makes me want to upgrade to Windows 10 and VS2015 to start using Dockers. I'd love to go back to Linux for serving my apps, and other open source programs and database servers.
I'm very happy with my current project right now! and see infinite possibilities with it today. I love the MVC platform and I'm not going back.
I wrote this because I still see the old webforms questions about Gridview, binding data to server controls, etc. And with all due respect, I think Code Project can use an upgrade in thread topics to cover these wonderful new technologies, and lead the way for all of us to benefit from them. I'd like to see some discussion on Node.js, and TypeScript.
I have many programmer friends that think MVC is sort of dumb, it's like classic ASP so whats the point? And I remember reading an earlier question as to why I should switch to MVC. So I hope this post provides a more convincing argument as to why MVC over webforms.
You guys and gals have a great weekend!
|
|
|
|
|
Thanks for sharing. I like the idea of creating something specific to new technologies for Code Lab where we can all experiment and discuss new topics
|
|
|
|
|
Thanks for the thumbs up John!
I went ahead and upgraded to Windows 10 from 8.1 and VS2015 from 2013 last Sunday.
I didn't know you can turn off almost all the BS in Win 10, and just run it straight.
It doesn't see all my ram, just half of the 32 gigs.
And the VS2015 is awesome, It has a JQuery and JavaScript lint checker that works very well.
My project is really cool now. When I publish, I can minify or ugligy all my Css, scripts, images to the production server, yet work with them raw in development for debugging.
So now on to the Sass, I need to learn that fast so I can create individual Sass files instead of Css and concat and minify them efficiently.
I guess I'm on my own now here with the new stuff. Perhaps others will read this post and jump on board.
I do like your thoughts on a place to talk about new stuff and the results were getting from it.
|
|
|
|
|
Hi
If you don't mind can you put some steps for each (Gulp , Sass, NPM) on how to install them and work on it. I am aware that I can get them online by googling but it will be more assuring if it is from someone we can check back.
|
|
|
|
|
Sure, give me a couple of days to compose an article instead of just posting here.
Thanks!
|
|
|
|
|