English 中文(简体)
每当我试图登记一个新物体时,它就把它退回数据库,或完全放弃。 我应该做些什么?
原标题:Everytime I try to register a new object, it returns to the database empty, or null. What should I do?

我认为我的看法是,我的看法是,从一个没有装满页的空洞田中获取价值,或者根本不产生,只是从《示范公约》中获取:

观点:

namespace Projeto_Vendedores.Models.ViewModels
{
    public class SellerFormViewModel
    {
        public Seller Seller { get; set; }
        public ICollection<Department> Departments { get; set; } 
    }
}

模式:

using Projeto_Vendedores.Migrations;

namespace Projeto_Vendedores.Models
{
    public class Seller
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public double BaseSalary { get; set; }
        public DateTime BirthDate { get; set; }
        public Department Department { get; set; }
        public int DepartmentId { get; set; }
        public ICollection<SalesRecord> Sales { get; set; } = new List<SalesRecord>();

        public Seller()
        {
        }
        public Seller(int id, string name, string email, double baseSalary, DateTime birhtDate, Department department)
        {
            Id = id;
            Name = name;
            Email = email;
            BaseSalary = baseSalary;
            BirthDate = birhtDate;
            Department = department;
        }       
    }
}

主计长:

using Microsoft.AspNetCore.Mvc;
using Projeto_Vendedores.Models;
using Projeto_Vendedores.Models.ViewModels;
using Projeto_Vendedores.Services;

namespace Projeto_Vendedores.Controllers
{
    public class SellersController : Controller
    {
        private readonly SellerService _sellerService;
        private readonly DepartmentService _departmentService;
        public SellersController(SellerService sellerService, DepartmentService departmentService)
        {
            _sellerService = sellerService;
            _departmentService = departmentService;
        }

        public IActionResult Index()
        {
            var list = _sellerService.FindAll();
            return View(list);
        }
        public IActionResult Create()
        {
            var departments = _departmentService.FindAll();
            var viewModel = new SellerFormViewModel { Departments = departments };
            return View(viewModel);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create(Seller obj)
        {
            _sellerService.Insert(obj);// it just add to the database and save changes
            return RedirectToAction(nameof(Index));
        }
    }
}

建立表格(POST):

@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@model Projeto_Vendedores.Models.ViewModels.SellerFormViewModel
@{
    ViewData["Title"] = "Create";
}
<h1>@ViewData["Title"]</h1>
<h4>Seller</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div class="form-group">
                <label asp-for="Seller.Name" class="form-label"></label>
                <input asp-for="Seller.Name" class="form-control" />
            </div>
            <div class="form-group">
                <label asp-for="Seller.Email" class="form-label"></label>
                <input asp-for="Seller.Email" class="form-control" />
            </div>
            <div class="form-group">
                <label asp-for="Seller.BirthDate" class="form-label"></label>
                <input asp-for="Seller.BirthDate" class="form-control" />
            </div>
            <div class="form-group">
                <label asp-for="Seller.BaseSalary" class="form-label"></label>
                <input asp-for="Seller.BaseSalary" class="form-control" />
            </div>
            <div class="form-group">
                <label asp-for="Seller.DepartmentId" class="control-label"></label>
                <select asp-for="Seller.DepartmentId" asp-items="@(new SelectList(Model.Departments,"Id", "Name"))" class="form-control"></select>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>     
    </div>
</div>

......

我只想正确阅读数据,但每次我尝试,申请都会停止,我只举一个例外,告诉我,第一个记录领域是无效的,其余也是无效的。

Here is the exception

问题回答

不要把任何东西交给SellerFormViewModel。 卖方然后试图对无卖方的财产进行约束。

尝试:

var departments = _departmentService.FindAll();
var viewModel = new SellerFormViewModel { Departments = departments, Seller = new Seller() };
return View(viewModel);

此外,并非每个人都有翻译语言的耐心,如果你以英文张贴技术细节,你会在今后得到更多的帮助。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签