locked
Bind certain properties of the navigation properties inside my action method RRS feed

  • Question

  • User-540818677 posted

    I have the following 2 model classes:-

     public Submission()
            {
                SubmissionQuestionSubmission = new HashSet<SubmissionQuestionSubmission>();
            }
    
            public int Id { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Npi { get; set; }
            public bool Independent { get; set; }
            public string Comment { get; set; }
    
            public virtual ICollection<SubmissionQuestionSubmission> SubmissionQuestionSubmission { get; set; }
        }
    
    
    public partial class SubmissionQuestionSubmission
        {
            public int SubmissionQuestionId { get; set; }
            public int SubmissionId { get; set; }
            public string Answer { get; set; }
    
            public virtual Submission Submission { get; set; }
        }

    and i created the following view model:-

    public class SubmissionCreate
        {
            public Submission Submission {set; get;}
            public IList<SubmissionQuestion> SubmissionQuestion { set; get; }
    
            public IList<SubmissionQuestionSubmission> SubmissionQuestionSubmission { set; get; }
    
        }

    then inside my view i only need to submit back the following fields:-

    @model LandingPageFinal3.ViewModels.SubmissionCreate
    <form asp-action="Create">
                    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                    <div class="form-group">
                        <label asp-for="Submission.FirstName" class="control-label"></label>
                        <input asp-for="Submission.FirstName" class="form-control" />
                        <span asp-validation-for="Submission.FirstName" class="text-danger"></span>
                    </div>
                    <div class="form-group">
                        <label asp-for="Submission.LastName" class="control-label"></label>
                        <input asp-for="Submission.LastName" class="form-control" />
                        <span asp-validation-for="Submission.LastName" class="text-danger"></span>
                    </div>
                    <div class="form-group">
                        <label asp-for="Submission.Npi" class="control-label"></label>
                        <input asp-for="Submission.Npi" class="form-control" />
                        <span asp-validation-for="Submission.Npi" class="text-danger"></span>
                    </div>
                    <div class="form-group form-check">
                        <label class="form-check-label">
                            <input class="form-check-input" asp-for="Submission.Independent" /> @Html.DisplayNameFor(model => model.Submission.Independent)
                        </label>
                    </div>
                    <div class="form-group">
                        <label asp-for="Submission.Comment" class="control-label"></label>
                        <textarea asp-for="Submission.Comment" class="form-control"></textarea>
                        <span asp-validation-for="Submission.Comment" class="text-danger"></span>
                    </div>
    
                    <div id="additionalInfo">
                        @for (var i = 0; i < Model.SubmissionQuestion.Count(); i++)
                        {
                            <label>@Model.SubmissionQuestion[i].Question</label><br />
                            <input asp-for="@Model.SubmissionQuestion[i].Question" hidden />
                            <textarea asp-for="@Model.SubmissionQuestionSubmission[i].Answer" class="form-control"></textarea>
                            <input asp-for="@Model.SubmissionQuestionSubmission[i].SubmissionQuestionId" hidden />
                            <br />
                        }
                    </div>

    so i define the following binding inside my post action method, to only bind the fields inside my view, as follow:-

    public async Task<IActionResult> Create([Bind(Submission.FirstName,Submission.LastName,Submission.Npi,Submission.Independent" +
                "Submission.Comment,SubmissionQuestionSubmission.Answer,SubmissionQuestionSubmission.SubmissionQuestionId")] SubmissionCreate sc )
            {

    but the sc.Submission and the sc.SubmissionQuestionSubmission will be null inside my action method... so not sure what is the minimum binding i should define?

    Friday, May 22, 2020 2:19 PM

All replies

  • User-474980206 posted

    You have two issues with your code.

    because you use IList in your view model, which is an interface, so the binder can not create an Instance. Your model constructor needs to do this.

    the bind attribute only supports property names, not dot notation.

    Friday, May 22, 2020 4:42 PM
  • User-540818677 posted

    You have two issues with your code.

    because you use IList in your view model, which is an interface, so the binder can not create an Instance. Your model constructor needs to do this.

    the bind attribute only supports property names, not dot notation.

    so how i can fix this issue? i am using IList because i am posting back a list of objects from my view to the action method.. what i need to use then?

    Thanks

    Friday, May 22, 2020 8:51 PM
  • User-854763662 posted

    Hi johnjohn123123 ,

    It seems that there is a  solution provided in the thread that you posted in the stackoverflow https://stackoverflow.com/questions/61951698/only-bind-certain-properties-of-the-navigation-properties-inside-my-action-metho

    Best Regards,

    Sherry

    Monday, May 25, 2020 10:12 AM