0
votes

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;}
1
at least an answer or a reason to downvote ... - badr slaoui
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 codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question It would help you a lot to make others understand your questions. - Aki la
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
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 may help you understand the issues (and in any case you should be using a view model!) - user3559349

1 Answers

0
votes

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"