2016-03-30 44 views
0

请让我试着尽我所能解释这一点。使用ActionLink增加财产价值

我已经试过研究这个,但我不知道如何正确地说出它来实现我正在寻找的东西。

我有这个我的网页上:Example

现在我希望发生的是,当用户点击了1“添加赢”的值增加(所以值将是5,如果用户点击链接) 。

我希望这发生,而不是被重定向到我的Edit视图。这似乎是没有意义/繁琐的必须填写表格,&保存表格,当我所需要做的就是将此数字的值增加1.

我该如何去做这件事?

我还没有在我的默认控制器Edit行动改变任何东西所以在这里,它是:

public ActionResult Edit(int? id) 
    { 
     if (id == null) 
     { 
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
     } 
     Team team = db.Teams.Find(id); 
     if (team == null) 
     { 
      return HttpNotFound(); 
     } 
     return View(team); 
    } 

    // POST: Teams/Edit/5 
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Edit([Bind(Include = "ID,TeamName,TotalWins, SeriesWins")] Team team) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Entry(team).State = EntityState.Modified; 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     return View(team); 
    } 

那么如何使用我ActionLink以增加数量的价值?

UPDATE:

HTML:

@Html.ActionLink("Add Win", "Edit", "Teams", new { id = 2 }, null) 
<tr> 
       @foreach (var item in Model.Where(x => x.TeamName == "example")) 
       { 
        <td class="text-center"> 
         <span id="teamLabel"> 
          @item.SeriesWins 
         </span> 

        </td> 
       } 
      </tr> 

任何帮助表示赞赏。

+1

我不会给出一个完整的解决方案,但为什么你不是简单地使用ajax?这就是这些xhr对象所关心的。如果你真的想使用asp.net mvc,那么请阅读部分视图。您可以提交部分内容,而无需再次重新渲染整个页面。也许这可以帮助你。 – Trickzter

+0

你应该增加使用JavaScript,然后提交表格 – Fals

+0

@Fals我发布了一个更新,包括我的HTML ..我怎么去做这与js,然后提交表单? –

回答

1

我找到了一种替代解决方案。

我在我的控制器中创建了一个自定义ActionResult以找到id并将+1添加到需要解决的属性。

public ActionResult AddOne(int id) 
    { 
     Team team = db.database.Find(id); 
     team.SeriesWins += 1; 
     UpdateModel(team); 
     db.SaveChanges(); 

     return RedirectToAction("Index"); 
    }