Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I want to pass ASP.NET MVC view (.cshtml) values to angular js controller. I am familiar with Angular js, But not on MVC. I have values in MVC cshtml. I want to pass that value to my angular controller. Please provide me some info or demo project or link which explains in detail.

i want somthing like below,

  1. Get value from mvc model and pass it to cshtml.
  2. from cshtml pass value to angular js controller and display in angular html page
    I do not want to use cshtml as my view. I want to get data from cshtml to angular controller and display in seperate html
share|improve this question
    
Could you share with us some sample? What have you come up with so far? Stackoverflow is meant to help with a problem you come across in your code. There are plenty of examples online. – Irshu Apr 25 at 2:33
    
MVC uses routing as the main driver behind its framework. So you need to wire up the controller action with the view/partial view and call the action via a WebApi like call. items/1/demo. This link should get you there dotnetcurry.com/aspnet-mvc/1012/… – yardpenalty Apr 25 at 2:35
    
try going through this article on CodeProject, it might help, codeproject.com/Articles/806029/… – Irshu Apr 25 at 2:36
    
i understand there are docs , but everywhere they are using webapi calls. i want to make only one call to get all the data from cshtml to angular controller. If you ahve any docs also it will help – apps Apr 25 at 2:38
    
I do not want to use cshtml as my view. I want to get data from cshtml to angular controller and display in seperate html – apps Apr 25 at 2:46

Using the very first tutorial I pulled up I grabbed this snippet:

var AwesomeAngularMVCApp = angular.module('AwesomeAngularMVCApp', ['ngRoute']);

AwesomeAngularMVCApp.controller('HomeController', HomeController);

var configFunction = function ($routeProvider) {
    $routeProvider.
        when('/Listings', {
            url: 'routesDemo/one'
        })
        .when('/Listing', {
            url: 'routesDemo/two'
        })
        .when('/Listings', {
            url: 'routesDemo/three'
        });
}
configFunction.$inject = ['$routeProvider'];

AwesomeAngularMVCApp.config(configFunction);

Now this how you link a view to a controller action in MVC:

using System.Web.Mvc;

namespace AwesomeAngularMVCApp.Controllers
{
    public class RoutesDemoController : Controller
    {
        public ActionResult One(string title)
        {
            var listings = db.Articles.Contain(title);
            return PartialView(listings, "..\Views\Shared\WhateverPartialView.cshtml");
        }

        [HttpPost]
        public async Task<ActionResult> Two(Article article)
        {
            if(ModelState.isValid){
                  _db.Add(article)
            }
            return View(article); //This one returns entire page
        }

        public JsonResult Three(string title)
        { 
            var listing = db.Articles.Where(t => t.Title == title).SingleOrDefault();
            return Json(listing, JsonRequestBehavior.AllowGet);
        }
    }
}

These partial views would be in the Views folder in a sub folder RoutesDemo

  1. one.cshtml GET
  2. two.cshtml POST ie: a href="#/Article/6" type=submit" class="btn"

    $("form").submit( function(){ $.ajax( function(url, data){ }); });

  3. three.cshtml GET ie

    $.ajax({ url: '@Url.Action("routeDemo", "three")', //url: baseUrl + url, data: { search: searchBlue.val() }, success: function (data) { $("#msg").html("Results for" + searchBlue.val()); searchBlue.searchMeme({ searchComplete: true }); $('#main').fadeOut(800, function () { $('#main').html("" + data + "").fadeIn().delay(800); }); searchBlue.val(""); }, error: function (xhr, status, error) { alert(error); } });

If you want to pass values to the angular controller use razor syntax and bind the value to the html input. The top of the view file will make it so angular knows what type of object to expect.

IE: Top of Two.cshtml

 @model AwesomeAngularMVCApp.Article

That should be it besides route.config for angular

If you want to handle the Model object using razor syntax check out this tutorial. Pretty cut and dry.If that is not enough I will show example of binding @Model.attribute/property to an html element after work tomorrow

EDIT

<div class="row-fluid" ng-controller="PersonDetailsController" ng-init="[email protected]">

Angular injects it in the scope during initialization, so you can refer to it as $scope.personId

share|improve this answer
    
here we are using cs.html to display....i donot want this. i will make it very simple. Now Let us say there is some value in cshtml file eg: <td> id </td> which is coming from mvc controller. now i want to pass this id to my angular controller. I do not want to disturb mvc controller or write any code in that. I just want to pass cshtml id value to angular controller. Here mvc and angular will act seperatly . I want to pass cshtml value to angular controller. Its only between cshtml and angular controller. How can i do this? – apps Apr 25 at 3:18
    
You dont have to disturb it. They are both on the client so what you can do is var id = @Model.Id using Razor. – yardpenalty Apr 26 at 22:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.