开发者

Persisting Lists in Model from View to Controller

I am pretty new to ASP.NET MVC so please be patient with me. I am having problems persisting a list in my model back to the controller when a user submits the form.

Here is a the scenario: This is a web app that allows users to create estimates for translation jobs. Each estimate has a list of language pairs. To give the users the ability to add languages pairs they need a list of all available language pairs.

My model looks something like this:

public class EstimateModel
{
    public tblEstimate Estimate { get; set; }
    public IEnumerable<tblEstimateLangPair> EstimateLanguagePairs { get; set; }
    public IEnumerable<tblLangPair> LanguagePairs { get; set; }

    public EstimateModel()
    {
        Estimate = new tblEstimate();
        EstimateLanguagePairs = new List<tblEstimateLangPair>();
        LanguagePairs = new List<tblLangPair>();
    }
}

This is basically a wrapper for objects that were created by the Entity Framework. In my controller I hydrate the estimate object and the two language pair lists and pass it to the view like so:

public ActionResult Edit(int estimateID)
{
    EstimateModel model = new EstimateModel();

    model.Estimate = db.tblEstimates.Single(t => t.EstimateID == estimateID);

    model.LanguagePairs = db.tblLangPairs.ToList();

    var query = from e in db.tblEstimateLangPairs
                join lp in db.tblLangPairs on e.LangPairID equals lp.ProductID
                where e.EstimateID == estimateID
                select new { e.EstimateID, e.LangPairID, lp.Language_pairs};

    model.EstimateLanguagePairs = query.ToList().ConvertAll(p => new tblEstimateLangPair
    {
        EstimateID = p.EstimateID,
        LangPairID = p.LangPairID,
        Language_pairs = p.Language_pairs
    });

    return PartialView(model);
}

In my 'partial' view I have controls for estimate object and for the language pair lists:

<%: Html.TextBoxFor(model => model.Estimate.JobNumber)%>
<%= Html.ListBoxFor(model => model.LanguagePairs, new SelectList(Model.LanguagePairs, "LangPairID", "Language_pairs"))%>
<%= Html.ListBoxFor(model => model.EstimateLanguagePairs, new SelectList(Model.EstimateLanguagePairs, "LangPairID", "Language_pairs"))%>

So far so good. I can populate these list boxes just. I can step through the controller code and see that my model has data there for all these object in my model that I am passing to the view.

My problem is when I try to submit this back to the controller to save any changes. The Model.Estimate object has no problem persisting all of it's properties back to the controller. When I look at the model in the controller method that will save changes, I see the list objects but their count is zero.

Save method in controller:

[HttpPost]
public ActionResult EditEstimate(EstimateModel model)
{
    db.tblEstimates.Attach(model.Estimate);
    db.ObjectStateManager.ChangeObjectState(model.Estimate, EntityState.Modified);

    foreach (tblEstimateLangPair langPair in model.EstimateLanguage开发者_运维百科Pairs)
    {
        db.tblEstimateLangPairs.Attach(langPair);
        db.ObjectStateManager.ChangeObjectState(langPair, EntityState.Modified);
    }

    db.SaveChanges();

    return Redirect("/Home/Index");
}

How do you persist lists, or collections in a model back to the controller when submitting form data? I believe my problem somewhere lies in how I use these ListBoxFor objects.

Any help would be greatly appreciated!


First load fiddler and watch your request being sent over. I'm guessing all the items from your listbox are not being posted - in that case there will be no model binding with it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜