Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ModelState无效,因为<;选择>;仅提供id,但需要其他属性_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# ModelState无效,因为<;选择>;仅提供id,但需要其他属性

C# ModelState无效,因为<;选择>;仅提供id,但需要其他属性,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我有一个型号CargoType,有id,name和其他型号。当向数据库添加新货物类型时,所有这些都是必需的,但当我添加货物的新对象时,这些对象具有金额和货物类型,我使用从现有对象中进行选择。Id是在此选择中真正选择的,但因为CargoType.Name为null,ModelState.isValid为false 货物型号: public class Cargo { public int Id { get; set; } public int Amount

我有一个型号
CargoType
,有
id
name
和其他型号。当向数据库添加新货物类型时,所有这些都是必需的,但当我添加
货物
的新对象时,这些对象具有
金额
货物类型
,我使用
从现有对象中进行选择。Id是在此选择中真正选择的,但因为
CargoType.Name
为null,ModelState.isValid为false

货物型号:

public class Cargo
    {
        public int Id { get; set; }
        public int Amount { get; set; }
        [Required]
        public CargoType CargoType { get; set; }
    }
public class CargoType
    {
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        public int Height { get; set; } //in millimeters
        public int Width { get; set; } //in millimeters
        public int Length { get; set; } //in millimeters
        public bool StackingAllowed { get; set; }
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("Id,PickUpAddress,DropOffAddress,PickUpTime,DropOffTime,OrderState,Cargo")] Order order)
    {
        if (order.Cargo != null)
        {
            foreach (var cargo in order.Cargo)
            {
                var cargoType = await _context.CargoTypes.FindAsync(cargo.CargoType.Id);
                cargo.CargoType = cargoType;
            }
        }
        //ModelState.Clear();
        if (ModelState.IsValid)
        {
            _context.Add(order);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(order);
    }
货物类型型号:

public class Cargo
    {
        public int Id { get; set; }
        public int Amount { get; set; }
        [Required]
        public CargoType CargoType { get; set; }
    }
public class CargoType
    {
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        public int Height { get; set; } //in millimeters
        public int Width { get; set; } //in millimeters
        public int Length { get; set; } //in millimeters
        public bool StackingAllowed { get; set; }
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("Id,PickUpAddress,DropOffAddress,PickUpTime,DropOffTime,OrderState,Cargo")] Order order)
    {
        if (order.Cargo != null)
        {
            foreach (var cargo in order.Cargo)
            {
                var cargoType = await _context.CargoTypes.FindAsync(cargo.CargoType.Id);
                cargo.CargoType = cargoType;
            }
        }
        //ModelState.Clear();
        if (ModelState.IsValid)
        {
            _context.Add(order);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(order);
    }
视图的一部分:

public class Cargo
    {
        public int Id { get; set; }
        public int Amount { get; set; }
        [Required]
        public CargoType CargoType { get; set; }
    }
public class CargoType
    {
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        public int Height { get; set; } //in millimeters
        public int Width { get; set; } //in millimeters
        public int Length { get; set; } //in millimeters
        public bool StackingAllowed { get; set; }
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("Id,PickUpAddress,DropOffAddress,PickUpTime,DropOffTime,OrderState,Cargo")] Order order)
    {
        if (order.Cargo != null)
        {
            foreach (var cargo in order.Cargo)
            {
                var cargoType = await _context.CargoTypes.FindAsync(cargo.CargoType.Id);
                cargo.CargoType = cargoType;
            }
        }
        //ModelState.Clear();
        if (ModelState.IsValid)
        {
            _context.Add(order);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(order);
    }
订单已列出货物

@model TransportationManagementSystem.Models.Order

[...]

@for (int i = 0; i < 2; i++)
            {
                <div class="form-group">
                    <label asp-for="Cargo[i].Amount" class="control-label"></label>
                    <input asp-for="Cargo[i].Amount" class="form-control" />
                    <span asp-validation-for="Cargo[i].Amount" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Cargo[i].CargoType" class="control-label"></label>
                    <select asp-for="Cargo[i].CargoType.Id" class="form-control" asp-items="ViewBag.CargoTypes"></select>
                    <span asp-validation-for="Cargo[i].CargoType" class="text-danger"></span>
                </div>
            }
@model-TransportationManagementSystem.Models.Order
[...]
@对于(int i=0;i<2;i++)
{
}
控制器:

public class Cargo
    {
        public int Id { get; set; }
        public int Amount { get; set; }
        [Required]
        public CargoType CargoType { get; set; }
    }
public class CargoType
    {
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        public int Height { get; set; } //in millimeters
        public int Width { get; set; } //in millimeters
        public int Length { get; set; } //in millimeters
        public bool StackingAllowed { get; set; }
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("Id,PickUpAddress,DropOffAddress,PickUpTime,DropOffTime,OrderState,Cargo")] Order order)
    {
        if (order.Cargo != null)
        {
            foreach (var cargo in order.Cargo)
            {
                var cargoType = await _context.CargoTypes.FindAsync(cargo.CargoType.Id);
                cargo.CargoType = cargoType;
            }
        }
        //ModelState.Clear();
        if (ModelState.IsValid)
        {
            _context.Add(order);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(order);
    }
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务创建([Bind(“Id、PickUpAddress、DropOffAddress、PickUpTime、DropOffTime、OrderState、Cargo”)]订单)
{
if(order.Cargo!=null)
{
foreach(订单中的var货物。货物)
{
var cargoType=wait_context.CargoTypes.FindAsync(cargo.cargoType.Id);
cargo.CargoType=CargoType;
}
}
//ModelState.Clear();
if(ModelState.IsValid)
{
_添加(顺序);
wait_context.SaveChangesAsync();
返回重定向到操作(名称(索引));
}
返回视图(订单);
}

我应该怎么做呢?

从模型中删除
public-CargoType-CargoType{get;set;}
(或者创建一个新的视图模型),并将其替换为类似
public-int-CargoTypeId{get;set;}
,这样您只需传递Id而不是整个对象。然后你的
会是
是的,我刚刚弄明白了。但是,还有其他解决办法吗?我感兴趣的是,是否有一种方法可以禁用ModelState中的验证?如果您只想在某些情况下验证
名称
字段,请删除
[必需]
属性。然后,在要使其成为必需的操作中,选中它并添加
ModelState.addmodelstatererror(“CargoType.Name”,“required”)
如果它没有填写,则调用
ModelState.IsValid
。我知道这与你的要求相反。。。但是没有
ModelState.DisableValidationForProperty
或任何东西。只需为您想要验证的东西建立一个视图模型——这就是视图模型的用途——一个针对这个特定视图的模型