English 中文(简体)
如何隐藏窗体中的外国密钥值
原标题:How to hide foreign key values from forms

我在数据库里有一个表格叫做“审查”,我用“实体框架”做了“审查”主计长,用阅读/写作动作和观点。所以它代表了所有脚架代码。

这是我的《编辑》专页

@model UniversityApp.Models.Review
@{
    ViewBag.Title = "Edit";
}

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Review</legend>

    @Html.HiddenFor(model => model.ReviewID)

    <div class="editor-label">
        @Html.LabelFor(model => model.MovieID, "Movie")
    </div>
    <div class="editor-field">
        @Html.DropDownList("MovieID", String.Empty)
        @Html.ValidationMessageFor(model => model.MovieID)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.UserID, "User")
    </div>
    <div class="editor-field">
        @Html.DropDownList("UserID", String.Empty)
        @Html.ValidationMessageFor(model => model.UserID)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Rating)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Rating)
        @Html.ValidationMessageFor(model => model.Rating)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Description)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Description)
        @Html.ValidationMessageFor(model => model.Description)
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

您可以看到脚手架机制增加了外国值密钥的编辑器。 我不想让用户在已经进行过审查后能够更改手动用户代号或电影代号, 因此我删除了这个代码。 但是在编辑复议条目时, 我在数据库 SaveCchanges () 中发现错误 :

外国密钥值无法插入, 因为相应的主密钥值不存在 。 [外国密钥限制名称 = Review_ User]

为了保护旧用户身份和电影身份价值,

此处为“审查主计长”中的编辑动作代码 :

public ActionResult Edit(int id)
{
    Review review = db.Reviews.Find(id);
    ViewBag.MovieID = new SelectList(db.Movies, "MovieID", "Title", review.MovieID);
    ViewBag.UserID = new SelectList(db.Users, "UserID", "UserName", review.UserID);
    return View(review);
}

[HttpPost]
public ActionResult Edit(Review review)
{
    if (ModelState.IsValid)
    {
        db.Entry(review).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    ViewBag.MovieID = new SelectList(db.Movies, "MovieID", "Title", review.MovieID);
    ViewBag.UserID = new SelectList(db.Users, "UserID", "UserName", review.UserID);
    return View(review);
}

<强 > EDIT:

I have tried the methods described, but I am still having problems. Now the error message I get is this: A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship. I don t know what am I doing wrong. The error is trigered on this line of code:

db.Entry(review).State = EntityState.Modified;
最佳回答

除了Cd Smith非常好的建议之外, 如果您不想以这种方式装饰自己的观景模型, 您也可以添加一个隐藏的字段, 这样看起来会像 :

@Html.HiddenFor(x => x.Review_UserID)

在窗体上您想要的每个 FK 等, 但不可见。 当然, 这和 < code> [ScoffoldColumn( false)] 装饰器当然的功能相似, tho 添加 < code> [HiddenInput( DisplayValue = fraud)] 属性对您来说是明确的 。

问题回答

在您的视图模式中用此在视图模式中说明您属性, 即您不想在视图中使用模板自动生成列

[ScaffoldColumn(false)]
public int ReviewID;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签