C# NET MVC:如何将linq lambda表达式与ViewModel一起使用以获取数据?

C# NET MVC:如何将linq lambda表达式与ViewModel一起使用以获取数据?,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,如何使用linq表达式使用ViewModel从其域模型之一获取数据?当我尝试获取与用户输入的项目id关联的inv_mast_uid时,我只得到一个0,而不是像10345这样的唯一整数。我确实从ViewModel中获取了所有其他必要的数据 这是一张显示我的三张桌子的图像: 以下是我尝试创建的ViewModel,用于向项目添加新的备用代码: public class AlternateCodeViewModel { [Key] [Column(Order = 0)] p

如何使用linq表达式使用ViewModel从其域模型之一获取数据?当我尝试获取与用户输入的项目id关联的inv_mast_uid时,我只得到一个0,而不是像10345这样的唯一整数。我确实从ViewModel中获取了所有其他必要的数据

这是一张显示我的三张桌子的图像:

以下是我尝试创建的ViewModel,用于向项目添加新的备用代码:

public class AlternateCodeViewModel
{


    [Key]
    [Column(Order = 0)]
    public int inv_mast_uid { get; set; }

    [Key]
    [Column(Order = 1)]
    [StringLength(50)]
    [Display(Name = "Alternate Code")]
    public string Alternate_Code_Item_ID { get; set; }

    [Key]
    [Column(Order = 2)]
    public int Alternate_Code_Brand_ID { get; set; }

    [Required]
    [StringLength(1)]
    [Display(Name = "Exact Flag")]
    public string Exact_Flag { get; set; }

    [Required]
    [StringLength(1)]
    [Display(Name = "Delete Flag")]
    public string Delete_Flag { get; set; }

    public string item_id { get; set; }


    public IEnumerable<web2_item_brands> web2_item_brands { get; set; }

    [ForeignKey("inv_mast_uid")]
    public inv_mast inv_mast { get; set; }

    public AlternateCodeViewModel()
    {
        Delete_Flag = "N";
        Exact_Flag = "Y";
    }
}
如果有帮助,这里有一个显示我的newCode对象及其缺少inv\u mast\u uid属性的图像:

编辑:根据建议,添加了以下附加信息 初始化视图的我的控制器:

ublic ActionResult AddAltCode()
    {    
        ViewBag.brandsdd = new SelectList(db.web2_item_brands.OrderBy(x => x.brand_name), "item_brand_uid", "brand_name");
        return View();
    }
我的看法是:

@using System.Web.Mvc.Html
@using System.Linq;
@model Non_P21_Quote_System_v1._0.ViewModels.AlternateCodeViewModel

@{
    ViewBag.Title = "AddAltCode";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Add Alternate Code</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })


    <div class="form-group">
        @Html.LabelFor(model => model.item_id, "Item ID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.item_id, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.item_id, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.web2_item_brands, "Brand Name", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("Alternate_code_brand_id", new SelectList(ViewBag.brandsdd, "Value", "Text") , htmlAttributes: new { @class = "form-control" })
                @*@Html.ValidationMessageFor(model => model.web2_item_brands, "", new { @class = "text-danger" })*@
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Alternate_Code_Item_ID, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Alternate_Code_Item_ID, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Alternate_Code_Item_ID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Add" class="btn btn-default" />
        </div>
    </div>
</div>
@使用System.Web.Mvc.Html
@使用System.Linq;
@模型非\u P21 \u报价\u系统\u v1.\u 0.ViewModels.alternativeDevice模型
@{
ViewBag.Title=“AddAltCode”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
添加备用代码
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.item_id,“item id”,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.item_id,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.item_id,“,new{@class=“text danger”}) @LabelFor(model=>model.web2_item_brands,“Brand Name”,htmlAttributes:new{@class=“control label col-md-2”}) @Html.DropDownList(“备用代码品牌id”,新选择列表(ViewBag.brandsdd,“值”,“文本”),htmlAttributes:new{@class=“form control”}) @*@Html.ValidationMessageFor(model=>model.web2_item_brands,“,new{@class=“text danger”})*@ @LabelFor(model=>model.Alternate_Code_Item_ID,htmlAttributes:new{@class=“control label col-md-2”}) @EditorFor(model=>model.Alternate_Code_Item_ID,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Alternate_Code_Item_ID,“,new{@class=“text danger”})
}

我的观点:

如果这是创建操作的结果,那么模型将始终返回您在模型中输入的任何内容

创建要发送到“创建”视图的模型时,必须在创建时设置属性inv\u mast\u uid

此外,即使不使用Html.HiddenFor编辑该属性,也必须将其包含在“创建”视图中。否则,在发送到视图时进入模型的内容将在返回时丢失

请求参数列表将只包括您在视图中明确指示的属性,就像任何html表单一样

控制器接口采用表单字段,并尝试将其与所示类中的字段相匹配。如果“创建视图”页面上不存在该字段,则不会尝试匹配它们

在这种情况下,包括初始化发送到视图的信息的代码以及视图本身的副本可能会有所帮助


我还将从模型中的IEnumerable中删除get/set,以便将其视为字段而不是属性。它可能与实体框架没有任何区别,但是get/set属性应该只反映数据库中的实际内容,除非您打算尝试序列化列表。

正如我从代码中看到的,您不能将“inv\u mast\u uid”和“inv\u mast inv\u mast”添加为整数属性。您需要将“inv_mst”和“web2_item_brands”作为对象添加到“web2_alternate_code”中,并在填充后单独保存它们。当您想要读取相关对象,需要调用“include”时,也是如此


因此,最好的方法是将这两个对象添加到“web2\u alternate\u code”中,并用“web2\u alternate\u code”构造函数实例化它们。之后,您可以在[HttpPost]上向这些对象添加任何数据。

因此,是否不可能让用户决定在一个视图中将与新的备用代码关联的项目?我最初是这样做的,用户在一个视图中选择项目,然后必须在另一个视图中为其设置备用代码,但我只是认为必须有一种方法可以在一个视图中完成所有这些。要让控制器根据视图返回的项目id找到所需的inv\u mast\u uid,我的意思是您需要包含视图的源代码,而不是视图本身。我确实将inv\u mast和web2\u item\u品牌作为我的web2\u备用代码模型的一部分作为导航属性。例如:公共inv_mast inv_mast{get;set;}。我认为使用ViewModel可以识别这一点。无论如何,我试图在Get方法中实例化一个“web2\u alternate\u code”对象以传递给要使用的视图,但我得到了一个“不能隐式转换类型IQueryable”错误。这是我的构造函数:web2\u item\u alternate\u code web2\u item\u alternate\u code=db.web2\u item\u alternate\u code.Include(w=>w.inv\u mast).Include(w=>w.web2\u item\u品牌);我一定是看错了你的建议。更改为:public virtual inv_mast inv_mast{get;set;};在构造函数中更改为:this.inv_mast=new HashSet();很有效!我非常感谢你不仅帮助了我,而且还教会了我一些新的东西。我没有完全按照你的指示去做,但一旦我理解了你的逻辑,我就能够让它发挥作用。我将更新我的原始帖子,展示你的解决方案,以便其他人可以使用。再次感谢。请将你的解决方案移至它的答案我自己的,谢谢。
@using System.Web.Mvc.Html
@using System.Linq;
@model Non_P21_Quote_System_v1._0.ViewModels.AlternateCodeViewModel

@{
    ViewBag.Title = "AddAltCode";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Add Alternate Code</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })


    <div class="form-group">
        @Html.LabelFor(model => model.item_id, "Item ID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.item_id, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.item_id, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.web2_item_brands, "Brand Name", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("Alternate_code_brand_id", new SelectList(ViewBag.brandsdd, "Value", "Text") , htmlAttributes: new { @class = "form-control" })
                @*@Html.ValidationMessageFor(model => model.web2_item_brands, "", new { @class = "text-danger" })*@
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Alternate_Code_Item_ID, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Alternate_Code_Item_ID, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Alternate_Code_Item_ID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Add" class="btn btn-default" />
        </div>
    </div>
</div>