简体   繁体   English

WCF---在 ASP.NET MVC 应用程序中使用 Linq 进行 CRUD 操作?

[英]WCF---Consuming CRUD operation using Linq in ASP.NET MVC application?

enter image description here在此处输入图像描述

First step...Opened WCF created IService :第一步...打开 WCF 创建IService

namespace CRUDOperationWCFMVC
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        bool CreateDetails(EmployeeDetails employeeDetails);
        [OperationContract]
        bool UpdateDetails(EmployeeDetails employeeDetails);
        [OperationContract]
        bool DeleteDetails(int id);
        [OperationContract]
        List<EmployeeDetails> GetDetails();
    }

    public class EmployeeDetails
    {
        [DataMember]
        public int EmpID { get; set; }
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Location { get; set; }
        [DataMember]
        public int? Salary { get; set; }
    }
}

Step 2: then I implemented service code:第2步:然后我实现了服务代码:

public class Service1 : IService1
{
    DataClasses1DataContext dcd = new DataClasses1DataContext();

    public bool CreateDetails(EmployeeDetails employeeDetails)
    {
        Nevint emp = new Nevint();
        emp.EmpID= employeeDetails.EmpID;
        emp.Name = employeeDetails.Name;
        emp.Location = employeeDetails.Location;
        emp.Salary = employeeDetails.Salary;
        dcd.Nevints.InsertOnSubmit(emp);
        dcd.SubmitChanges();
        return true;
    }

    public bool DeleteDetails(int id)
    {
       var delete = (from v in dcd.Nevints where v.EmpID==id select v).FirstOrDefault();
        dcd.Nevints.DeleteOnSubmit(delete);
        dcd.SubmitChanges();
        return true;
    }

    public List<EmployeeDetails> GetDetails()
    {
        List<EmployeeDetails> details = new List<EmployeeDetails>();
        var select= (from v in dcd.Nevints select v);

        foreach (var i in select)
        {
            EmployeeDetails emp = new EmployeeDetails();
            emp.EmpID = i.EmpID;
            emp.Name = i.Name;
            emp.Location = i.Location;
            emp.Salary = i.Salary;
            details.Add(emp);
        }

        return details;
    }

    public bool UpdateDetails(EmployeeDetails employeeDetails)
    {
        var update = (from v in dcd.Nevints.ToList() where employeeDetails.EmpID==v.EmpID select v).FirstOrDefault();
        update.EmpID = employeeDetails.EmpID;
        update.Name = employeeDetails.Name;
        update.Location = employeeDetails.Location;
        update.Salary = employeeDetails.Salary;
        dcd.SubmitChanges();
        return true;
    }
}

Step 3: then I add linq to sql, opened my ASP.NET MVC project for consuming, and added a controller and wrote this code:第 3 步:然后我将 linq 添加到 sql,打开我的 ASP.NET MVC 项目进行消费,并添加了一个 Z594C18AB5 和 3DDA53F2C0304 代码

namespace ConsumingClient.Controllers
{
    public class EmpdetailsController : Controller
    {
        ServiceReference1.Service1Client serobj=new ServiceReference1.Service1Client();
        ServiceReference1.EmployeeDetails empdetails=new ServiceReference1.EmployeeDetails();
            
        // GET: Empdetails
        public ActionResult Index()
        {
            List<employee> lstemp = new List<employee>();
            var result = serobj.GetDetails();
            foreach (var i in result)
            {
                employee emp = new employee();
                empdetails.EmpID = i.EmpID;
                empdetails.Name = i.Name;
                empdetails.Location = i.Location;
                empdetails.Salary = i.Salary;
                lstemp.Add(emp);
            }
            return View(result);
        }

        // GET: Empdetails/Details/5
        public ActionResult Details(int id)
        {
           Employees emp = new Employees();
            
            return View();
        }

        // GET: Empdetails/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Empdetails/Create
        [HttpPost]
        public ActionResult Create(Employees employees)
        {
            try
            {
                // TODO: Add insert logic here
                empdetails.EmpID=employees.EmpID;
                empdetails.Name = employees.Name;
                empdetails.Location = employees.Location;
                empdetails.Salary = employees.Salary;
                serobj.CreateDetails(empdetails);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Empdetails/Edit/5
        public ActionResult Edit(int id)
        {
            Employees emp = new Employees();
            var result = serobj.GetDetails().FirstOrDefault(a=>a.EmpID==id);
            emp.EmpID = result.EmpID;
            emp.Name = result.Name;
            emp.Location = result.Location;
            emp.Salary = result.Salary;

            return View(emp);
        }

        // POST: Empdetails/Edit/5
        [HttpPost]
        public ActionResult Edit(Employees employees)
        {
            try
            {
                // TODO: Add update logic here
                empdetails.EmpID = employees.EmpID;
                empdetails.Name = employees.Name;
                empdetails.Location = employees.Location;
                empdetails.Salary = employees.Salary;
                serobj.UpdateDetails(empdetails);
                return RedirectToAction("Index");
                
            }
            catch
            {
                return View(employees);
            }
        }

        // GET: Empdetails/Delete/5
        public ActionResult Delete(int id)
        {
            Employees emp = new Employees();
            var result = serobj.GetDetails().FirstOrDefault(a=>a.EmpID==id);
            
            emp.EmpID = result.EmpID;
            emp.Name = result.Name;
            emp.Location = result.Location;
            emp.Salary = result.Salary;

            return View(emp);
        }

        // POST: Empdetails/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                serobj.DeleteDetails(id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View(id);
            }
        }
    }
}

Data was displaying fine.数据显示正常。 I can create data.我可以创建数据。

However, when I click on edit and delete, I'm getting an error:但是,当我单击编辑和删除时,出现错误:

ERROR Message "Server Error in '/' Application.错误消息“'/'应用程序中的服务器错误。

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32)' in 'ConsumingClient.Controllers.EmpdetailsController'.参数字典包含一个 null 条目,用于“ConsumingClient.Controllers.EmpdetailsController”中方法“System.Web.Mvc.ActionResult Edit(Int32)”的不可为空类型“System.Int32”的参数“id” An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.可选参数必须是引用类型、可空类型或声明为可选参数。

Parameter name: parameters参数名称:参数

This error is thrown if you attempt to call this controller action and you do not specify the id either in the path portion or as query string parameter.如果您尝试调用此 controller 操作并且未在路径部分或查询字符串参数中指定 id,则会引发此错误。 Since your controller action takes an id as parameter you should make sure that you always specify this parameter.由于您的 controller 操作将 id 作为参数,因此您应确保始终指定此参数。

Make sure that when you are requesting this action you have specified a valid id in the url:确保当您请求此操作时,您已在 url 中指定了有效 ID:

http://example.com/somecontroller/Edit/123

If you are generating an anchor, make sure there's an id:如果您正在生成锚点,请确保有一个 id:

@Html.ActionLink("Edit", "somecontroller", new { id = "123" })

If you are sending an AJAX request, also make sure that the id is present in the url.如果您要发送 AJAX 请求,还要确保该 ID 存在于 url 中。

If on the other hand the parameter is optional, you could make it a nullable integer:另一方面,如果参数是可选的,则可以将其设为可为空的 integer:

public ActionResult Edit(int? id)

but in this case you will have to handle the case where the parameter value is not specified.但在这种情况下,您将不得不处理未指定参数值的情况。 https://coderedirect.com/questions/197477/mvc-the-parameters-dictionary-contains-a-null-entry-for-parameter-k-of-non-n https://coderedirect.com/questions/197477/mvc-the-parameters-dictionary-contains-a-null-entry-for-parameter-k-of-non-n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM