0

Here am trying to use EditorTemplates for displaying list entity. Below i mention code structure. PostResume is parent model.

public class PostResume
{
    .....
    [UIHint("ResumeURL")]
    public List<ResumeURL> ResumeURLs { get; set; }
    .....
}

ResumeURL is child model. This entity i need to display while user click add button.

public class ResumeURL
    {        
        public string URL { get; set; }
        public string Name { get; set; }
    }

View part. In my parent view Create.cshtml i mention below script for adding child view HTML.

$('#add_url_button').on('click', function () {
            $.get('/PostResume/AddURL').done(function (html) {                
                $('#url_from').append(html);
            });
        });

Parent view Action Methods.

public ActionResult AddURL()
        {
            var postResume = new PostResume();
            postResume.ResumeURLs.Add(new ResumeURL());    
            return View(postResume);
        }

I defined two views for displaying HTML in Parent view. In ~/View/PostResume/EditorTemplates location i defined ResumeURL.cshtml view

@model IEnumerable<Nccoam_JobProject.Models.ResumeURL>
@foreach (var ResumeURL in Model)
{
    <div class="form-group">
        <label for="URL">URL</label>
        @Html.TextBoxFor(model => ResumeURL.URL, new { @class = "form-control input" })
        @Html.ValidationMessageFor(model => ResumeURL.URL, "", new { @class = "text-danger" })
    </div>
    <div class="form-group">
        <label for="Name">Name</label>
        @Html.TextBoxFor(model => ResumeURL.Name, new { @class = "form-control input" })
        @Html.ValidationMessageFor(model => ResumeURL.Name, "", new { @class = "text-danger" })
    </div>
}

And also i create AddURL.cshtml view for display HTML part while clicking AddURL button in master view.

@model Nccoam_JobProject.Models.PostResume
@{
    Layout = null;
}
@Html.EditorFor(x => x.ResumeURLs)

This is my post action, in that am not getting exact value in an EditorTemplate.

[HttpPost]
        public ActionResult Create(PostResume model)
        {
            return View();
        }

Here i mention image of my view. enter image description here

As per above image, user need to click on "+Add URL" button then only two fields of child template need to be display. And user also click multiple times for adding URL. So i am not getting that List of fields value in Post Actions.

HTML for editor template view. URL entity : <input class="form-control input" id="ResumeURLs_ResumeURL_URL" name="ResumeURLs.ResumeURL.URL" value="" type="text"> Name entity : <input class="form-control input" id="ResumeURLs_ResumeURL_Name" name="ResumeURLs.ResumeURL.Name" value="" type="text">

Thanks in an Advance.

Jeet Bhatt
  • 744
  • 1
  • 7
  • 22
  • So what is your current behavior ? What specifically is the problem you are running into ? – Shyju Jun 12 '18 at 02:39
  • If you are wanting to dynamically add new collection items, refer the answers [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) and [here](http://stackoverflow.com/questions/40539321/partial-view-passing-a-collection-using-the-html-begincollectionitem-helper/40541892#40541892) for some options –  Jun 12 '18 at 02:45
  • Also in your http post action, why do you need all 3 params ? I still do not understand your requirements. but i think you need only `PostResume` as the method parameter – Shyju Jun 12 '18 at 02:46
  • @Shyju Actually i will try with different options that's why i mention all three parameter. Only PostRersume model parameter is required. My issue is when i add multiple fields like URL and Name. Suppose 3 times i add means ResumeURL[0].URL,ResumeURL[0].Name..... till ResumeURL[2].URL,ResumeURL[2].Name... But that model value i am getting null in PostResume model entity...public List ResumeURLs { get; set; } Below is HTML generated. – Jeet Bhatt Jun 12 '18 at 03:00
  • Your data will not bind because the `name` attributes have no relationship to your model –  Jun 12 '18 at 03:03
  • @StephenMuecke What changes i made in that view? Can you please help me on that. – Jeet Bhatt Jun 12 '18 at 03:09
  • Read the links I gave you :) –  Jun 12 '18 at 03:12

0 Answers0