我正在使用codefirst方法开发一个mvc5应用程序。我正在努力更新在视图中作为动态输入生成的列表(字符串列表)。
我有两个模型,Atribute和AtributeDetail,它们具有一对多的关系,以及一个名为AttributeViewModel的视图模型。与typevalue属性绑定的数据(字符串列表)保存到AtributeDetail表,而其他数据保存到Atribute表。
这是代码
属性模型
public class Atribute
{
[Key]
public int AtributeId { get; set; }
public string AtributeCode { get; set; }
public string AtributeName { get; set; }
public string Remarks { get; set; }
public bool StatusId { get; set; }
}
AtributeDetail Model
public class AtributeDetail
{
[Key]
public int AtributeDetailId { get; set; }
public string AtDetailVal { get; set; }
public int AtributeId { get; set; }
public virtual Atribute Atribute { get; set; }
}
AttributeViewModel
public class AttributeViewModel
{
public int AtributeId { get; set; }
public string AtributeCode { get; set; }
public string AtributeName { get; set; }
//for dropdownlist
public int SelectedValue { get; set; }
public IEnumerable<SelectListItem> Values
{
get
{
return new[]
{
new SelectListItem { Value = "1", Text = "Text" },
new SelectListItem { Value = "2", Text = "Select" },
};
}
}
public List<String> typevalue { get; set; }
public string Remarks { get; set; }
public bool StatusId { get; set; }
}
_EditAtribute PartialView
@using (Html.BeginForm("Update", "Atribute"))
{
@Html.TextBoxFor(a=>a.AtributeCode)
@Html.TextBoxFor(a=>a.AtributeName)
@Html.DropDownListFor(x => x.SelectedValue, Model.Values, "Select a Type")
<div>
<input type="text" id="newtypevalue" /></div>
<div>
<input type="button" value="Add Text" id="addtypevalue" /></div>
<div id="typevaluelist">
// Generate inputs for existing items and in case of returning the view
@foreach(var item in Model.typevalue)
{
<div class="typevalue">
<input type="text" name="typevalue" value="@item" />
<button type="button" class="delete">Delete</button>
</div>
}
</div>
<div>
<input type="submit" value="Save" id="btn" class="btn btn-success"/>
</div>
}
// Hidden template for adding new items (outside the form element)
<div id="new" style="display:none;">
<div class="typevalue">
<input type="text" name="typevalue" />
<button type="button" class="delete">Delete</button>
</div>
</div>
//scripts for adding and deleting items
$('#addtypevalue').click(function() {
var clone = $('#new').clone().children('div'); // copy the template
clone.find('input').val($('#newtypevalue').val()); // update text
$('#typevaluelist').append(clone); // add it to the DOM
$('#newtypevalue').val(''); // clear initial text box
});
$('#typevaluelist').on('click', '.delete', function() {
$(this).closest('.typevalue').remove(); // remove it from the DOM
});
<!--Script for ddl-->
$('#SelectedValue').change(function () {
if ($("#SelectedValue option:selected").val() == 1) {
$('#newdiv').hide();
}
else {
$('#newdiv').show();
}
});
<!--/Script for ddl-->
更新操作结果
public ActionResult Update(AttributeViewModel avm)
{
if (ModelState.IsValid)
{
var objContext = new KnittingdbContext();
//blog.Id = 1;
Atribute atribute = new Atribute
{
AtributeId=avm.AtributeId,
AtributeCode=avm.AtributeCode,
AtributeName=avm.AtributeName,
Remarks=avm.Remarks,
StatusId=avm.StatusId
};
objContext.Atributes.Attach(atribute);
var obJBlog = objContext.Entry(atribute);
obJBlog.Property(a => a.AtributeCode).IsModified = true;
obJBlog.Property(a => a.AtributeName).IsModified = true;
obJBlog.Property(a => a.Remarks).IsModified = true;
obJBlog.Property(a => a.StatusId).IsModified = true;
List<AtributeDetail> listAd = objContext.AtributeDetails.Where(a => a.AtributeId==avm.AtributeId).ToList();
foreach (AtributeDetail a in listAd)
{
objContext.AtributeDetails.Remove(a);
}
objContext.SaveChanges();
AtributeDetail newAd;
if (avm.typevalue != null)
{
foreach (String txt in avm.typevalue)
{
newAd = new AtributeDetail
{
AtributeId=avm.AtributeId,
AtDetailVal = txt
};
objContext.AtributeDetails.Add(newAd);
}
}
objContext.SaveChanges();
TempData["SuccessEdit"] = "Saved Sucessfully";
return RedirectToAction("AtributeIndex");
}
else
{
return PartialView("_EditAtribute", avm);
}
}
这里我正在做的更新是,从AttributDetail表中删除与特定AtributeId相关的先前列表并保存当前绑定的数据。这有效。但我知道这不是更新列表的正确方法,因为未更改的元素不应该更新。请以正确的方式指导我。
我有另一个问题。如何设置一个选定的值(在检查条件后显示一个选定的文本)来自&#34;值&#34;编辑视图中AtributeViewModel中的属性。
请帮我解决这两个问题。提前谢谢!