1

One of my views presents zero to many objects of four different entity classes.

My goal is to have an easy way to edit one common value for all four entity types.

There is only one submit button for the whole view, this submit button invokes an ActionResult where changes made to all editable values shall be stored like this (notice: this is not working because signature seems to be invalid):

    public ActionResult SaveBloombergTickers(IEnumerable<StockPosition> stockpositions, IEnumerable<BondPosition> bondposition, ...)
    {
        if (ModelState.IsValid)
        {
            foreach (var stockposition in stockpositions)
            {
                db.Entry(stockposition).State = EntityState.Modified;
                db.SaveChanges();
            }
            foreach (var bondposition in bondpositions)
            {
                db.Entry(bondposition).State = EntityState.Modified;
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        return View("Index");
    }

Question 1: I kinda feel this is a stupid way to accomplish what I want. Unfortunatly, I am not aware of any "best practice" for this. Are you?

Question 2: What would a valid signature for the ActionResult in this case look like?


Solution (thanks to David L for giving me the idea):

1) Attach necessary data (ID of current element) to the textbox when building the View:

<input type="text" class="updateBloombergTicker StockPosition" 
data-key="@stockposition.StockPositionID"  style="width: 100px;" 
value="@stockposition.BloombergID"/>

2) On textbox blur, fire corresponding Ajax-Event (I use a function "AJAX" for that) and hand over necessary info to controller:

$(".updateBloombergTicker").blur(function () {

    $this = $(this);
    if ($this.hasClass("StockPosition")) {
        AJAX("../Excel/UpdateStockPositionTicker",
            {
                key: $this.attr("data-key"),
                ticker: $this.val()
            }
        );
    }

    if ($this.hasClass("BondPosition")) {
    .......

3) Controller does the rest:

    [HttpPost]
    public ActionResult UpdateStockPositionTicker(string key, string ticker)
    {
        if (ModelState.IsValid)
        {
            var stockposition = db.StockPositions.Find(Convert.ToInt32(key));
            stockposition.BloombergID = ticker;
            db.SaveChanges();
    ....
peter
  • 2,103
  • 7
  • 25
  • 51
  • Hm one "solution" just came to my mind.. one "save" button for each object. That would be really ugly, though. – peter May 23 '13 at 16:20

2 Answers2

1

I think the overall approach may be questionable.

If you are looking to actively manage the edited state of your objects, perhaps you need a more robust, ajax solution.

I would personally do a "focusOut" binding and, upon focusOut, calling a unique action to update that status and ONLY that status.

This may require additional architectural overhead that you cannot or will not add to your project, but it will make it more flexible.

David L
  • 32,885
  • 8
  • 62
  • 93
  • Hm this could be a solution. Like an Ajax Postback when one of the editable textboxes loses focus. I'll give it a try! – peter May 23 '13 at 18:05
  • @peter Exactly! That will allow each textbox to handle it's own state, rather than forcing the entire form to post at once. – David L May 23 '13 at 18:09
0

You should have ViewModel which is independent from your Entity Classes. In controller you should provide all the necessary updates to any entity you need.

paramosh
  • 2,258
  • 1
  • 15
  • 23
  • Could you please give an example for how the controller would have to look like? How would a signature, capable of zero to many objects of four entities look like? – peter May 23 '13 at 16:38
  • look at this discussion http://stackoverflow.com/questions/4764011/multiple-models-in-a-view – paramosh May 23 '13 at 17:18
  • The discussion there is focussed on how to display two different models, this I have achieved already. I just dont know how to process the information in the postback so as to update all objects. – peter May 23 '13 at 18:03