gpt4 book ai didi

c# - ASP.NET MVC : How to use linq lambda expression with a ViewModel to get data?

转载 作者:太空宇宙 更新时间:2023-11-03 12:56:47 26 4
gpt4 key购买 nike

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

这是显示我的三个表的图像: enter image description here

这是我尝试创建一个 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";
}
}

这是我的 Controller 方法:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddAltCode(AlternateCodeViewModel avm)
{
if (ModelState.IsValid)
{

var newCode = new web2_item_alternate_code
{
inv_mast_uid = avm.inv_mast_uid,
Alternate_Code_Item_ID = avm.Alternate_Code_Item_ID,
Alternate_Code_Brand_ID = avm.Alternate_Code_Brand_ID,
Delete_Flag = avm.Delete_Flag,
Exact_Flag = avm.Exact_Flag
};

db.web2_item_alternate_code.Add(newCode);
db.SaveChanges();
return RedirectToAction("AltCodeIndex");
}
return View();
}

如果它有帮助,这是一张显示我的 newCode 对象的图像,它缺少 inv_mast_uid 属性:

enter image description here

编辑:按照建议,已添加以下附加信息

初始化 View 的我的 Controller :

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>

我的观点的图像: enter image description here

最佳答案

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

所以最好的方法是在“web2_alternate_code”中添加这两个对象,并使用“web2_alternate_code”构造函数实例化它们。之后,您可以将任何数据添加到 [HttpPost] 上的那些对象。

关于c# - ASP.NET MVC : How to use linq lambda expression with a ViewModel to get data?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33614991/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com