0

I've this data model

public partial class UsersInfo
{
    public int UserID { get; set; }
    public string Name_f { get; set; }
    public string Name_l { get; set; }
    public string Photo { get; set; }
    public bool ActiveAccount { get; set; }
}

public partial class Employee
{
    public int EmpID { get; set; }
    public int BranchID { get; set; }
    public virtual UsersInfo UsersInfo { get; set; }
}

and i'm rendering this form

@model LoanApp.Models.DataBaseModel.Employee
@using (Html.BeginForm((string)ViewBag.Contoler, "Employe", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.Partial("~/Views/Shared/_userInfo.cshtml",Model.UsersInfo)
    <label class="control-label">يعمل في فــرع</label>
    @Html.DropDownList("BranchID", null, htmlAttributes: new { @class = "form-control" })
    <input type="submit" value="Save" class="btn btn default" />
}

and this is my partial view

@model LoanApp.Models.DataBaseModel.UsersInfo
@Html.TextBoxFor(x => x.Name_f, new { @class = "form-control", @placeholder = "الاسم الاول", @required = "" })
@Html.TextBoxFor(x => x.Name_l, new { @class = "form-control", @placeholder = "الاسم الاخير", @required = "" })
@Html.TextBoxFor(x => x.PhoneNumber, new { @class = "form-control", @required = "", @number = "true" })

I want my post edit method to has the object from userinfo instead of null, I'm not sure what i'm missing

I've one to one relation between table employee and users info

enter image description here

magdi
  • 17
  • 9
  • Where is your form ? How does your HttpPost action method looks like and what is your expected behavior ? – Shyju Dec 12 '17 at 22:54
  • I think complex properties are not automatically serialized. – TheVillageIdiot Dec 12 '17 at 22:55
  • 1
    You need to show your POST method. Your generating form controls for `ChildViewModel` which will not match your model. Use an `EditorTemplate`, not a partial so the `name` attributes are correctly prefixed –  Dec 12 '17 at 23:06
  • And if you did want to use a partial, then you need to pass the `HtmlFieldPrefix` to the partial as explained in [this answer](https://stackoverflow.com/questions/29808573/getting-the-values-from-a-nested-complex-object-that-is-passed-to-a-partial-view/29809907#29809907) –  Dec 12 '17 at 23:08
  • i've updated mu code and and added an image – magdi Dec 12 '17 at 23:32
  • Change your `_userInfo.cshtml` partial to `UsersInfo.cshtml` (t match the name of the model, and then move it to the `/Views/Shared/EditorTemplates` folder, and then replace `@Html.Partial()` with `@Html.EditorFor(m => m.UsersInfo)` and your form controls will be correctly named. (look at the `name` attributes before and after you make the change to understand) –  Dec 12 '17 at 23:36
  • And remove your `new { @required = "" }` - that makes no sense –  Dec 12 '17 at 23:40
  • @StephenMuecke , i did every thing and for some reason i'm still getting null on empolyee.userinfo .... – magdi Dec 12 '17 at 23:58
  • Then you did not do it correctly :). Your existing code generates `` but in order to bind to your model it needs to be `name= "UsersInfo.Name_f` which it will do if you followed my instructions. –  Dec 13 '17 at 01:30
  • But FGS, your editing data so always use a view model. And I just looked at your image and your excluding the `UsersInfo` from binding anyway so of course it will be `null` –  Dec 13 '17 at 01:32
  • @StephenMuecke , any change you can show me an example how it should look like ? i following your instructions and the name and the id property are formatted the like this UsersInfo.Name_f, UsersInfo.Name_l ? – magdi Dec 13 '17 at 02:27
  • Just start by removing the `[Bind]` attribute from your POST method to get it working (Sorry, I do not have time now to correct all the other awful code and bad practices you have) –  Dec 13 '17 at 02:30
  • @StephenMuecke , i thank you for your time , u been very helpful :) – magdi Dec 13 '17 at 03:04
  • @magdi you should be carefull with the bindings, maybe here its no problem to remove it, since you are using all the properties, but it can be a huge security gap in another action fi you do so. I suggest you to follow the link in the Microsoft-Generated Comment(go.microsoft.com/fwlink/?linkid=317598) to learn more about binding! – bnu Dec 13 '17 at 09:05

1 Answers1

0

The problem is your post method [Bind(Include = "some property")]. Which means you specified the list of properties only included in the model object, Other properties are not included in the model object.

  • There are far more errors in OP code than just the `[Bind]` attribute, and changing that would not do anything without all the other necessary changed –  Dec 14 '17 at 01:37