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
public class Names
{
public int NameId { get; set; }
public string Username { get; set; }
}
, a conrtoller
[HttpPost]
public ActionResult EditforModel(Names Name)
{
if (ModelState.IsValid)
{
db.Entry(Name).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(Name);
}
adding and editing view adding is working well, the question is about editing I use
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend> legend </legend>
@Html.EditorForModel()
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
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
@Html.HiddenFor(model => model.NameId)
@Html.EditorFor(model => model.Username)
but using it is dangerous because some user can post wrong Id throgh post-request.
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 :(