0

I have a client as :

 public partial class Client
{

    public Client()
    {
        ResponsablesClients = new HashSet<ResponsablesClient>();
    }

    [Key]
    [StringLength(10)]
    [ScaffoldColumn(false)]
    public string CodeClient { get; set; }

    [StringLength(50)]
    [DisplayName("Raison sociale")]
    [Required]
    public string Name { get; set; }

    public virtual ICollection<ResponsablesClient> ResponsablesClients { get; set; }
}

and the ResponsablesClient as :

public partial class ResponsablesClient
{
    public int Id { get; set; }

    [StringLength(10)]
    [ScaffoldColumn(false)]
    public string CodeClient { get; set; }

    [StringLength(50)]
    [DisplayName("Nom complet")]
    [Required]
    public string Nom { get; set; }

    public virtual Client Client { get; set; }
}

my controller create (httpget) method :

public ActionResult Create()
    {
        return View();
    }

When i want to create a new client i use a strongly typed view of type client, and in that view i add dynamically strongly typed partial views . In the create httpPost the Client model i get contains Client properties but not a list of responsablesClients , here is the minimized view :

<div class="form-group">
        @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBoxFor(model => model.Name, new {@class="form-control"})
            @Html.ValidationMessageFor(model => model.Name)
        </div>
    </div>

<div class="form-horizontal" style="margin-top: 50px">
        <h4>Responsables</h4>
        <hr/>
        <div id="responsables">
            <div id="responsable0">
                @Html.Partial("_ResponsableCreate",new ResponsablesClient())
            </div>
        </div>
    </div>

so my question how can i get that list of ResponsablesClient without creating a custom view as :

public Client  client {get;set;}
public List<ResponsablesClient> responsablesClient {get;set;}
tereško
  • 58,060
  • 25
  • 98
  • 150
badr slaoui
  • 1,023
  • 11
  • 27
  • at least an answer or a reason to downvote ... – badr slaoui Jan 23 '15 at 20:32
  • You should give more explanation and be more precise. Start your question with your question and background to make people understand it. Throwing code here without explaining it would not help us to help you. you should probably read http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ It would help you a lot to make others understand your questions. – Aki la Jan 23 '15 at 20:48
  • I suppose the guy that downvote you would have more to say, but that doesn't matter if you edit your text soon. – Aki la Jan 23 '15 at 20:55
  • The need to show your partial view, but unless your using the `BeginCollectionItem` helper, using a partial view wont help because your generating duplicate `name` attributes and invalid html. [This answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) may help you understand the issues (and in any case you should be using a view model!) –  Jan 23 '15 at 21:53

1 Answers1

0

In case someone have the same question i found a solution maybe not the best but it did it . every time i make an ajax call to render the partial view i pass an id that i pass to view via ViewData which is the total number of rendered views with conventions naming here is the code :

the call to render partial view

@Html.Partial("_ResponsableCreate",new ResponsablesClient(),new ViewDataDictionary(){new KeyValuePair<string, object>("id",count)})

then in the partial view :

@{
var variable = ViewData["id"];
}
<input type="hidden" name="ResponsablesClients.Index" value="@variable"/>

@Html.TextBoxFor(model => model.Nom, new {@class = "form-control", Name = "ResponsablesClients[" + @variable + "].Nom"})

i retrieve id via ViewData i use this Id and put it in a hidden input , it's an mvc convention for non sequential lists , so you don't have to set successive Id's , then the name of each input follow the convention :

name = "PropertyNameOfTheListInTheModel[@variable].propery"

badr slaoui
  • 1,023
  • 11
  • 27
  • As you stated _maybe not the best_. There are numerous issues why this could fail (for example, you seem to base the indexer value on the count of items in the collection so it you delete an item the add a new item your will get duplicate values and binding will fail). I suggest you study the link in my comment above. –  Jan 24 '15 at 01:06
  • you're right , in case i delete divs the id will no longer be the count but substring of the Id : example : id = "example23" newId = parseInt(id.substring(7)) + 1 and this here where come the utility of the hidden input – badr slaoui Jan 24 '15 at 01:43