在没有模型MVC4的情况下将值从文本框传递到局部视图

时间:2014-03-05 11:05:09

标签: c# asp.net-mvc-4 partial-views

我有一个视图,里面有一个部分视图。当我提交表单时,动作ShowDetail可以完美地运行并显示在它应该的表格中。只有一件事,我希望第一行是空的,除了一个单元格(id为“startvalue”的单元格)。如果我只是在td-tag中键入一些值就可以了。但我想要来自@ Html.TextBox(“PresentValue”)的值。

我尝试了一些JQUERY但它没有用,它有点阻止了我的其他JS函数(不知道为什么)..

所以我的问题是,如果我可以从@ Html.TextBox(“PresentValue”)获取值并将其发送到提交表单中吗?

我试过的jQuery:

$(document).on('click', '#test', function() 
{
  var startValue = $("#PresentValue");
  $("#startvalue").Append(startValue);
}

我的控制器:

public ActionResult ShowDetail(FormCollection form)
{
  List<Calculation> cList = new List<Calculation>();
  Calculation calc = new Calculation();
  calc.Date = Convert.ToDateTime(form["startdate"]);
  calc.InvoiceAmount = 2000;
  calc.InterestRate = Convert.ToDouble(form["InterestRate"]);
  calc.InterestAmount = (Convert.ToDouble(form["PresentValue"]) * Convert.ToDouble(form["InterestRate"]) / 360 * 30);
  calc.Amortization = (2000 - (Convert.ToDouble(form["PresentValue"]) * Convert.ToDouble(form["InterestRate"]) / 360 * 30));
  calc.PresentValue = Convert.ToDouble(form["PresentValue"]) - calc.Amortization;
  cList.Add(calc);
  for (int i = 0; i < Convert.ToInt32(form["PaymentPeriods"]); i++)
  {
    Calculation calcBefore = cList.Last();
    calc = new Calculation();
    calc.Date = calcBefore.Date.AddMonths(1);
    calc.InvoiceAmount = 2000;
    calc.InterestRate = Convert.ToDouble(form["InterestRate"]);
    calc.InterestAmount = (calcBefore.PresentValue * Convert.ToDouble(form["InterestRate"]) / 360 * 30);
    calc.Amortization = (calc.InvoiceAmount - (calcBefore.PresentValue * calc.InterestRate / 360 * 30));
    calc.PresentValue = calcBefore.PresentValue - calc.Amortization;
    cList.Add(calc);
  }
  return PartialView("ShowDetail", cList);
}

我的偏见:

<tr>
  <td align="center">
  </td>
  <td align="center">
  </td>
  <td align="center">
  </td>
  <td align="center">
  </td>
  <td align="center">
  </td>
  <td align="center" id="startValue">
    //Here do I want the value from @Html.Textbox("PresentValue")
  </td>
</tr>
@foreach (var item in Model) 
{
  <tr>
    <td align="center">
        @Html.DisplayFor(modelItem => item.Date)
    </td>
    <td align="center">
        @Html.DisplayFor(modelItem => item.InvoiceAmount)
    </td>
    <td align="center">
        @Html.DisplayFor(modelItem => item.InterestRate)
    </td>
    <td align="center">
        @Html.DisplayFor(modelItem => item.InterestAmount)
    </td>
    <td align="center">
        @Html.DisplayFor(modelItem => item.Amortization)
    </td>
    <td align="center">
        @Html.DisplayFor(modelItem => item.PresentValue)
    </td>
  </tr>
}

在我的视图中(在BeginForm中):

@Html.TextBox("PresentValue")

2 个答案:

答案 0 :(得分:1)

尝试将您的jquery片段更改为以下内容:

$(document).on('click', '#test', function() 
{
  var startValue = $("#PresentValue").val();
  $("#startvalue").text(startValue);
}

答案 1 :(得分:0)

尝试

$(document).ready(function() 
{
  var startValue = $("#PresentValue").val();
  $("#startvalue").text(startValue);
}