Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help to explain Readonly\ScaffoldColumn(false)
    primarykey
    data
    text
    <p>Please help with such a question and do not judge strictly because I'm a newbie in MVC: I've got a model for storing names of users by ID in my DB</p> <pre><code>public class Names { public int NameId { get; set; } public string Username { get; set; } } </code></pre> <p>, a conrtoller</p> <pre><code>[HttpPost] public ActionResult EditforModel(Names Name) { if (ModelState.IsValid) { db.Entry(Name).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(Name); } </code></pre> <p>adding and editing view adding is working well, the question is about editing I use</p> <pre><code> @using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt; legend &lt;/legend&gt; @Html.EditorForModel() &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } &lt;div&gt; @Html.ActionLink("Back to List", "Index") &lt;/div&gt; </code></pre> <p>to edit my model. when trying to go to this view I see an editor for both Id and Username, but if i fill Id - I've got error, because there is no Entry in DB with such Id. Ok.Let's look for attributes to hide an editor. [ScaffoldColumn(false)] is something like a marker whether to render an editor for Id or not. applaying it to my model I've got "0" id posting from my View.Try another attr. [ReadOnly(true)] makes a field a readonly-field. But at the same time I've got "0" in posting Id. Modifying a view I placed an editors for each field in model</p> <pre><code>@Html.HiddenFor(model =&gt; model.NameId) @Html.EditorFor(model =&gt; model.Username) </code></pre> <p>but using it is dangerous because some user can post wrong Id throgh post-request.</p> <p>I can't use [ScaffoldColumn(false)] with applying Id at [Httppost] action of the controller,by searching appropriate user-entry in DB, because the name was changed.. I can't believe @Html.HiddenFor is the only way out.But can't find one :(</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload