.NET danna un warenko

NJack

Active member
  • Jun 4, 2017
    471
    144
    43
    .NET Routing wala,

    man menna me route(url) ekata giyama

    http://localhost:50383/Customers/Details

    mata menna me wage error ekak enawa,

    [ArgumentException: The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'Vidly.Controllers.CustomersController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters]

    menna me route ekata giyama menna me wage error ekak enawa

    http://localhost:50383/Customers/Details/1

    The model item passed into the dictionary is of type 'Vidly.Models.Customer', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Vidly.Models.Customer]'.

    menna mage code tika

    CustomerController.cs

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Vidly.Models;
    
    namespace Vidly.Controllers
    {
        public class CustomersController : Controller
        {
            // GET: Customers
            public ActionResult Index() {
                var customers = GetCustomers();
    
                return View(customers);
            }
    
    
            public ActionResult Details(int id)
            {
    
                var customer = GetCustomers().SingleOrDefault(c => c.Id == id);
                if (customer == null) {
                    return HttpNotFound();
                }
    
                return View(customer);
            }
    
            private IEnumerable<Customer> GetCustomers() {
                return new List<Customer>
                {
                    new Customer { Id = 1, Name = "John Smith" },
                    new Customer {Id =2, Name="Mary Kom"}
    
                };
            }
    
    
    
        }
    }

    Details.cshtml

    Code:
    @model IEnumerable<Vidly.Models.Customer>
    
    @{
        ViewBag.Title = "Customer";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    
    
    @if (!Model.Any())
    {
        <p>Please endter a valid URL</p>
    }
    else
    {
        <table class="table table-bordered table-hover">
    
            <thead>
    
                <tr>
    
                    <th>Customer</th>
    
                </tr>
    
            </thead>
    
            <tbody>
                @foreach (var customer in Model)
                {
    
                    <tr>
    
                        <td>@Html.ActionLink(customer.Name, "Details", "Customers", new { id = customer.Id }, null)</td>
                        <li>
    
                    </tr>
                }
    
            </tbody>
    
        </table>
    }

    RoutingConfig.cs

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    
    namespace Vidly
    {
        public class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
    
    
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
            }
        }
    }

    mokada scene eka kiyala balapnko, Stackoverflow dammama un duplicate kiyala ain kala :(
     

    deltamoon

    Active member
  • Mar 6, 2007
    246
    155
    43
    :no: ethakota data type error ekak enawa ++

    public ActionResult Details(string id)
    {

    var custId= int.parse(id);

    var customer = GetCustomers().SingleOrDefault(c => c.Id == custId);
    if (customer == null) {
    return HttpNotFound();
    }

    return View(customer);
    }
     

    nalaka526

    Active member
  • Aug 4, 2006
    462
    176
    43
    Arangala
    Controller eke tiyana Method tunata debug points daala balanna call wenne mona welawal waladaida kiyala

    1st issue eka enne view ekata map wechcha method ekak controller eke nethi nisa wenna one

    Me method eka controller ekata daala balanna:

    HTML:
    public ActionResult Details() {
                var customers = GetCustomers();
    
                return View(customers);
            }


    PM me if you need more help
     
    Last edited:

    Jolly_Roger

    Well-known member
  • May 2, 2009
    10,600
    1,606
    113
    Colombo XOR Matara
    http://localhost:50383/Customers/Details/1

    me URL eka gahuwama ube method eka call wela customer obect ekak return wenawa View ekata.
    habai View eke Model eka widiyata thiyenne "IEnumerable<Vidly.Models.Customer>"
    a kiyanne uba Customer List ekak return karanna ona controller action eken. habai logic eke widiyata nam eka Id ekata inne 1 customer nisa View eke uda mehema dapan

    @model Vidly.Models.Customer
     

    saja

    Well-known member
  • Jan 8, 2007
    16,127
    2
    10,766
    113
    Home Sweet Home
    .NET Routing wala,

    man menna me route(url) ekata giyama

    http://localhost:50383/Customers/Details

    mata menna me wage error ekak enawa,

    [ArgumentException: The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'Vidly.Controllers.CustomersController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters]
    (
    You are expecting an id parameter in your URL but you aren't supplying one. Such as:

    http://localhost:50383/Customers/Details/12<< missing
     

    rosharavinda

    Well-known member
  • Dec 28, 2007
    1,588
    166
    63
    Bellanwila
    mcn umbe details view ekak mama dakke na. ekata mehema daanna

    HTML:
    @model TestApp.Models.Customer
    
    @{
        ViewBag.Title = "Details";
    }
    
    <h2>Details</h2>
    
    <div>
        <h4>Customer</h4>
        <hr />
        <dl class="dl-horizontal">
            <dt>
                @Html.DisplayNameFor(model => model.Name)
            </dt>
    
            <dd>
                @Html.DisplayFor(model => model.Name)
            </dd>
    
        </dl>
    </div>
    <p>
        @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
        @Html.ActionLink("Back to List", "Index")
    </p>

    Index view ekata mehema daaanna

    HTML:
    @model IEnumerable<TestApp.Models.Customer>
    
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th></th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
                @Html.ActionLink("Details", "Details", new { id=item.ID }) |
                @Html.ActionLink("Delete", "Delete", new { id=item.ID })
            </td>
        </tr>
    }
    
    </table>


    Aniwa hariyai. Try karala kiyanne. Oya CSS classnam bootstrap ewa mcn. ewa ain karanna oyata one nam. Anith eka mcn as a best practice controllers PLURAL wain liyanna epa. CustomersController newei CustomerController. That's the microsoft standard. Ewath dan danma purudu wenna.
     
    Last edited:

    NJack

    Active member
  • Jun 4, 2017
    471
    144
    43
    public ActionResult Details(string id)
    {

    var custId= int.parse(id);

    var customer = GetCustomers().SingleOrDefault(c => c.Id == custId);
    if (customer == null) {
    return HttpNotFound();
    }

    return View(customer);
    }


    wada na bn :(
    Controller eke tiyana Method tunata debug points daala balanna call wenne mona welawal waladaida kiyala

    1st issue eka enne view ekata map wechcha method ekak controller eke nethi nisa wenna one

    Me method eka controller ekata daala balanna:

    HTML:
    public ActionResult Details() {
                var customers = GetCustomers();
    
                return View(customers);
            }


    PM me if you need more help

    balanna one :nerd: :)
    mcn umbe details view ekak mama dakke na. ekata mehema daanna

    HTML:
    @model TestApp.Models.Customer
    
    @{
        ViewBag.Title = "Details";
    }
    
    <h2>Details</h2>
    
    <div>
        <h4>Customer</h4>
        <hr />
        <dl class="dl-horizontal">
            <dt>
                @Html.DisplayNameFor(model => model.Name)
            </dt>
    
            <dd>
                @Html.DisplayFor(model => model.Name)
            </dd>
    
        </dl>
    </div>
    <p>
        @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
        @Html.ActionLink("Back to List", "Index")
    </p>

    Index view ekata mehema daaanna

    HTML:
    @model IEnumerable<TestApp.Models.Customer>
    
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th></th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
                @Html.ActionLink("Details", "Details", new { id=item.ID }) |
                @Html.ActionLink("Delete", "Delete", new { id=item.ID })
            </td>
        </tr>
    }
    
    </table>


    Aniwa hariyai. Try karala kiyanne. Oya CSS classnam bootstrap ewa mcn. ewa ain karanna oyata one nam. Anith eka mcn as a best practice controllers PLURAL wain liyanna epa. CustomersController newei CustomerController. That's the microsoft standard. Ewath dan danma purudu wenna.

    DisplayNameFor kiyana function eka mokakda? :shocked:
     

    rosharavinda

    Well-known member
  • Dec 28, 2007
    1,588
    166
    63
    Bellanwila
    mcn poddak therune naththam oya google karala balanna.

    DisplayNameFor kiyanne HTML helper ekak :
    https://stackoverflow.com/questions...-for-displayfor-and-displaynamefor-in-asp-net

    oyata oyage thiyena widiyatama onenam karanna one display.cshtml eke

    @model TestApp.Models.Customer

    @if (Model == null)
    {
    <p>Please endter a valid URL</p>
    }

    <td>@Html.ActionLink(Model.Name, "Details", "Customers", new { ID = Model.ID }, null)</td>

    onna ohoma karala balanna hariyai
     

    NJack

    Active member
  • Jun 4, 2017
    471
    144
    43
    mcn umbe details view ekak mama dakke na. ekata mehema daanna

    HTML:
    @model TestApp.Models.Customer
    
    @{
        ViewBag.Title = "Details";
    }
    
    <h2>Details</h2>
    
    <div>
        <h4>Customer</h4>
        <hr />
        <dl class="dl-horizontal">
            <dt>
                @Html.DisplayNameFor(model => model.Name)
            </dt>
    
            <dd>
                @Html.DisplayFor(model => model.Name)
            </dd>
    
        </dl>
    </div>
    <p>
        @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
        @Html.ActionLink("Back to List", "Index")
    </p>

    Index view ekata mehema daaanna

    HTML:
    @model IEnumerable<TestApp.Models.Customer>
    
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th></th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
                @Html.ActionLink("Details", "Details", new { id=item.ID }) |
                @Html.ActionLink("Delete", "Delete", new { id=item.ID })
            </td>
        </tr>
    }
    
    </table>


    Aniwa hariyai. Try karala kiyanne. Oya CSS classnam bootstrap ewa mcn. ewa ain karanna oyata one nam. Anith eka mcn as a best practice controllers PLURAL wain liyanna epa. CustomersController newei CustomerController. That's the microsoft standard. Ewath dan danma purudu wenna.

    Ade badu wada bn :D thanks a lot :love: