我是Entity Framework和Asp.Net的新手,我正试图弄清楚如何将特定字段的默认值设置为FormView。这是基本代码:
<asp:FormView runat="server" ID="addObjForm"
ItemType="MySpace.Models.Obj"
InsertMethod="insertObj" DefaultMode="Insert"
RenderOuterTable="false" OnItemInserted="obj_ItemInserted">
<InsertItemTemplate>
<fieldset>
<ol>
<asp:DynamicEntity ID="DynamicEntity1" runat="server" Mode="Insert" />
</ol>
<asp:Button ID="Button1" runat="server" Text="Insert" CommandName="Insert" />
<asp:Button ID="Button2" runat="server" Text="Cancel" CausesValidation="false" OnClick="cancelButton_Click" />
</fieldset>
</InsertItemTemplate>
</asp:FormView>
背后的代码:
public void insertObj()
{
var item = new Obj();
TryUpdateModel(item);
if (ModelState.IsValid)
{
using (WebContext db = new WebContext())
{
db.Objects.Add(item);
db.SaveChanges();
}
}
}
假设我必须为使用外键引用其父对象的子对象插入数据,并通过GET将此外键传递给Web表单。如何自动将此值设置为FormView?