Click here to Skip to main content
15,879,535 members
Home / Discussions / ASP.NET
   

ASP.NET

 
QuestionBad Behavior Pin
BobbyStrain15-Jun-16 9:35
BobbyStrain15-Jun-16 9:35 
AnswerRe: Bad Behavior Pin
jkirkerx15-Jun-16 10:18
professionaljkirkerx15-Jun-16 10:18 
GeneralRe: Bad Behavior Pin
BobbyStrain15-Jun-16 12:44
BobbyStrain15-Jun-16 12:44 
GeneralRe: Bad Behavior Pin
Nathan Minier16-Jun-16 1:18
professionalNathan Minier16-Jun-16 1:18 
GeneralRe: Bad Behavior Pin
BobbyStrain16-Jun-16 6:24
BobbyStrain16-Jun-16 6:24 
GeneralRe: Bad Behavior Pin
Nathan Minier17-Jun-16 1:57
professionalNathan Minier17-Jun-16 1:57 
GeneralRe: Bad Behavior Pin
BobbyStrain17-Jun-16 5:50
BobbyStrain17-Jun-16 5:50 
Question@Url is saying "the name url doesn't exist in the current context" ASP.MVC 4 Pin
indian14314-Jun-16 10:02
indian14314-Jun-16 10:02 
Hi All,

I am trying to call an action method on a button click, I am trying to create and declare my button as below, basically it is in the Index.cshtml page or View from there I want to redirect my page to the "CreateUser.cshtml", my Controller name is "UserController" and my Action method is "CreateUser".
My Controllers are in Controllers folder and My user related Views are in "\Views\User" folder under root folder
<input type="button" value="Create User" onclick="location.href='@Url.Action("Create", "User")'" />
And my View where the button is there is:
@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>
                <input type=&quot;button&quot; value=&quot;Create User&quot; onclick=&quot;location.href=&#39;@Url.Action(&quot;Create&quot;, &quot;User&quot;)&#39;&quot; />
            </td>
        </tr>
    </table>
</div>    


And Controller is:
<pre>
    public class UserController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetUsers()
        {
            var dbContext = new MVCDBContext();

            List<User> listOfUsers;
            listOfUsers = dbContext.Users.ToList();

            return Json(listOfUsers, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /User/Details/5
        public ActionResult Details(int id)
        {

            return View();
        }

        //
        // GET: /User/Create

        public ActionResult CreateUser()
        {
            return View();
        }

        //
        // POST: /User/Create
        [HttpPost]
        public ActionResult CreateUser(FormCollection collection)
        {
            try
            {
                var dbContext = new MVCDBContext();
                User user = new User();
                user.UserId = 1;
                user.UserName = "Abdul";

                dbContext.Users.Add(user);
                dbContext.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /User/Update/5
        public ActionResult UpdateUser(int id)
        {
            return View();<br />
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult UpdateUser(int id, FormCollection collection)
        {
            try
            {
                var dbContext = new MVCDBContext();

                User user = new User();
                user.UserId = 1;
                user.UserName = "Abdul";

                user = dbContext.Users.Where(i => i.UserId == 1).FirstOrDefault();
                user.UserName = "Abdul Aleem";

                dbContext.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /User/Delete/5

        public ActionResult DeleteUser(int id)
        {
            return View();
        }

        //
        // POST: /User/Delete/5

        [HttpPost]
        public ActionResult DeleteUser(int id, FormCollection collection)
        {
            try
            {
                var dbContext = new MVCDBContext();

                User user = new User();

                IQueryable<User> Users = dbContext.Users.Where(i => i.UserId == 1);

                foreach (User a in Users)
                {
                    dbContext.Users.Remove(a);
                }

                dbContext.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }

And Web.config within View Folder is:
<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>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

  <system.web>
    <httpHandlers>
      <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>

<pre>
<!--
    Enabling request validation in view pages would cause validation to occur
    after the input has already been processed by the controller. By default
    MVC performs request validation before a controller processes the input.
    To change this behavior apply the ValidateInputAttribute to a
    controller or action.
-->
<pages
    validateRequest=&quot;false&quot;
    pageParserFilterType=&quot;System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot;
    pageBaseType=&quot;System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot;
    userControlBaseType=&quot;System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot;>
  <controls>
    <add assembly=&quot;System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot; namespace=&quot;System.Web.Mvc&quot; tagPrefix=&quot;mvc&quot; />
  </controls>
</pages>



<system.webserver>
<validation validateintegratedmodeconfiguration=""false"">
<handlers>
  <remove name=&quot;BlockViewHandler&quot;/>
  <add name=&quot;BlockViewHandler&quot; path=&quot;*&quot; verb=&quot;*&quot; preCondition=&quot;integratedMode&quot; type=&quot;System.Web.HttpNotFoundHandler&quot; />
</handlers>





Not only the @Url my View is giving error at @model that model doesn't exist and saying ViewBag doesn't exist.

If you need my model
namespace MVCWithWebApiApp.Models
{
    public class User
    {
        private int _userId;
        public int UserId
        {
            get { return _userId; }
            set { _userId = value; }
        }

        private string _userName;
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

    }
}

Please help me I am new to ASP.Net MVC
Thanks,

Abdul Aleem

"There is already enough hatred in the world lets spread love, compassion and affection."


modified 14-Jun-16 16:49pm.

AnswerRe: @Url is saying "the name url doesn't exist in the current context" ASP.MVC 4 Pin
jkirkerx15-Jun-16 7:24
professionaljkirkerx15-Jun-16 7:24 
GeneralRe: @Url is saying "the name url doesn't exist in the current context" ASP.MVC 4 Pin
indian14315-Jun-16 8:06
indian14315-Jun-16 8:06 
GeneralRe: @Url is saying "the name url doesn't exist in the current context" ASP.MVC 4 Pin
jkirkerx15-Jun-16 13:09
professionaljkirkerx15-Jun-16 13:09 
GeneralRe: @Url is saying "the name url doesn't exist in the current context" ASP.MVC 4 Pin
indian14315-Jun-16 23:05
indian14315-Jun-16 23:05 
GeneralRe: @Url is saying "the name url doesn't exist in the current context" ASP.MVC 4 Pin
indian14316-Jun-16 9:08
indian14316-Jun-16 9:08 
GeneralRe: @Url is saying "the name url doesn't exist in the current context" ASP.MVC 4 Pin
jkirkerx16-Jun-16 10:37
professionaljkirkerx16-Jun-16 10:37 
GeneralRe: @Url is saying "the name url doesn't exist in the current context" ASP.MVC 4 Pin
indian14316-Jun-16 16:52
indian14316-Jun-16 16:52 
GeneralRe: @Url is saying "the name url doesn't exist in the current context" ASP.MVC 4 Pin
indian14316-Jun-16 22:45
indian14316-Jun-16 22:45 
GeneralRe: @Url is saying "the name url doesn't exist in the current context" ASP.MVC 4 Pin
jkirkerx17-Jun-16 5:54
professionaljkirkerx17-Jun-16 5:54 
GeneralRe: @Url is saying "the name url doesn't exist in the current context" ASP.MVC 4 Pin
indian14317-Jun-16 9:15
indian14317-Jun-16 9:15 
GeneralRe: @Url is saying "the name url doesn't exist in the current context" ASP.MVC 4 Pin
jkirkerx19-Jun-16 7:07
professionaljkirkerx19-Jun-16 7:07 
Question.NET Oracle.DataAccess.Client Pin
Karan_TN14-Jun-16 3:52
Karan_TN14-Jun-16 3:52 
AnswerRe: .NET Oracle.DataAccess.Client Pin
John C Rayan14-Jun-16 4:22
professionalJohn C Rayan14-Jun-16 4:22 
Question.net Pin
Member 1257929012-Jun-16 7:59
Member 1257929012-Jun-16 7:59 
AnswerRe: .net Pin
Richard MacCutchan12-Jun-16 23:00
mveRichard MacCutchan12-Jun-16 23:00 
AnswerRe: .net Pin
F-ES Sitecore12-Jun-16 23:32
professionalF-ES Sitecore12-Jun-16 23:32 
AnswerRe: .net Pin
Richard Deeming13-Jun-16 2:05
mveRichard Deeming13-Jun-16 2:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.