简体   繁体   中英

Passing value from textbox to partial view without model MVC4

I have a view and inside that a partialview. When I submit my form the action ShowDetail works perfectly and shows in a table like it should. There's just one thing, I want to first row to be empty except for one cell (the one with id="startvalue"). If i just type in some value inside the td-tag it works fine. But I want the value from @Html.TextBox("PresentValue").

I have tried with some JQUERY but it didn't work and it kind a blocked my other JS-functions (don't know why)..

So my question is if I possible can take the value from @Html.TextBox("PresentValue") and send it into on submitting a form?

The jQuery I tried:

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

My Controller:

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);
}

My PartialView:

<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>
}

In my View (inside a BeginForm):

@Html.TextBox("PresentValue")

Try to change your jquery snippet to something like this:

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

Try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM