Reputation: 21
I am entirely new in MVC. I just want to display list of story title on my page using mvc with wcf service. After debugging my application throw an exception:
Could not find default endpoint element that references contract 'ServiceReference1.IService1' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
Codes are as follow :
HomeController.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcMovie.Controllers
{
public class HomeController : Controller
{
// ServiceReference1.Service1Client cs = new ServiceReference1.Service1Client();
public ActionResult Index()
{
//ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
ServiceReference1.Service1Client cs = new ServiceReference1.Service1Client();
List<ServiceReference1.Story> tm = cs.GetBollywoodStory();
return View();
}
}
}
Index.cshtml
@{
ViewBag.Title = "Index";
}
<h2>My Movie List</h2>
<p>Hello from our View Template!</p>
@*<%n ServiceReference1.Service1Client cs = new ServiceReference1.Service1Client();%>
*@Name
<%foreach (ServiceReference1.Story dr in tm)
{%>
<%=dr["Name"].ToString() %>
<% } %>
Please help me out.....
Upvotes: 2
Views: 2480
Reputation: 5622
You shouldn't write code logic in your View!
In your controller you should get all data you need for you view and put it in some ViewModel class. Then from your model display it to your client.
Let say you create some ViewModel called MovieViewModel;
public class MovieViewModel {
public string Name { get; set; }
... //rest of properties
}
Then you populate you viewmodel in your controller and send it to your view:
Controller
public ActionResult Index()
{
//ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
ServiceReference1.Service1Client cs = new ServiceReference1.Service1Client();
List<ServiceReference1.Story> tm = cs.GetBollywoodStory();
//populate viewmodel
var movies = new List<MovieViewModel>();
foreach(var item in tm) {
movies.Add(new MovieViewModel {
Name = item.Name;
...
});
}
return View(movies);
}
When you have populated model passed to your view you can render it to your client:
View
@model IEnumerable<MvcMovie.<folder of your viewmodels>.MovieViewModel>
@{
ViewBag.Title = "Index";
}
<h2>My Movie List</h2>
<p>Hello from our View Template!</p>
@foreach(var movie in Model) {
@Html.DisplayNameFor(m => m.Name)
@Html.DisplayFor(m => m.Name)
}
*Note the model reference on the start of the view!
You should learn about Razor, and also about MVC helpers... This can be start point but you need to find some good tutorials about MVC.
maybe this: http://www.microsoftvirtualacademy.com/training-courses/developing-asp-net-mvc-4-web-applications-jump-start
Upvotes: 2