简体   繁体   English

发送数据后将其保留为表格

[英]keeping the data in a form after it has been sent

I have a form that you enter data into and it performs a calculation on it and give an answer. 我有一个表格,您可以在其中输入数据并对其进行计算并给出答案。 what i want to do is for it to keep the data in the form so that you can quickly repost so that you don't have to change all the data. 我想要做的是将其保留在表单中,以便您可以快速重新发布,从而不必更改所有数据。 but I cant keep coming up with the error of it not existing, which I suppose is correct until the form has been posted! 但我无法继续提出它不存在的错误,我认为这是正确的,直到表单已发布!

@{
  var total = 0m;
  var totalMessage = "";


    if (IsPost)
    {
        var age = Request["frmage"].AsInt(0);
        var weight = Request["frmweight"].AsDecimal();
        var SerCre = Request["frmSerCre"].AsDecimal();
        var sexfactor = Request["frmGender"]== "M" ? 1.23m : 1.04m;

        total =Convert.ToDecimal ((((140 - age)*weight)* sexfactor )/SerCre ) ;

        totalMessage =  total.ToString("0.00") + "(ml/min) ";
    }


}
<div class="memberRegistration">
<form method="post">

<p>
    <label class="formLabel">Age:</label> in years
    <input class="formTextField" type="text" name="frmAge" size="3" value="@age"/>
</p>
<p>
    <label class="formLabel">Weight:</label> in Kg (1st = 6.35kg)
    <input class="formTextField" type="text" name="frmWeight" value="@weight"/>
</p>
<p>
    <label class="formLabel">Serum Creatinine:</label> in μmol/L
    <input class="formTextField" type="text" name="frmSerCre" value="@SerCre"/>

</p>
<p>

<label class="fieldLabel">Gender:</label>
    <select name="frmGender" id="select" value="@sexfactor">
        <option value="M">Male</option>
        <option value="F">Female</option>
    </select>

</p>
<p><input type="submit" value="Calculate" /></p>
</form>
<p>Calculated creatinine clearance <b>@totalMessage</b></p>
</div>

Try this 尝试这个

var age = 0;

    if (IsPost)
    {
        age = Request["frmage"].AsInt(0);

    }

<input class="formTextField" type="text" name="frmAge" size="3" value="@age"/>

But normally it would be better to use a model to hold your values, then in your controller you pass those values back again to your form 但是通常最好使用模型来保存您的值,然后在控制器中将这些值再次传递回表单

启用页面和控件的ViewState,还使用aspx控件,而不是HTML。

I don't thing that i realy understand the Question because the default thing is that the web page keeps it's view state so the data will still be the same after the post back but here's the solution : 我并不真正理解问题,因为默认情况是网页保持其视图状态,因此回发后数据仍将保持不变,但这是解决方案:

you can simply use ASP Controls because it keep it's view state or you can give each control of them it's value in the C# , you can assign to each control it's value back 您可以简单地使用ASP控件,因为它可以保持其视图状态,也可以在C#中为每个控件提供其值,您可以将其值分配回每个控件

Hope I Helped 希望我能帮上忙

Since you are using ASP.NET MVC Razor, what you can do is, do not submit the form using <input type="submit" value="Calculate" /> , instead change it to a simple button like 由于您正在使用ASP.NET MVC Razor,因此您可以做的是,不要使用<input type="submit" value="Calculate" />提交表单,而是将其更改为一个简单的按钮,例如

      <input type="button" value="Calculate" onclick="javascript:Submitform();" />

and submit the form using Jquery POST.eg like below 并使用Jquery POST.eg提交表单,如下所示

   function SubmitForm(){
      var formData = $("form").serialize() ;
      var submitUrl = 'yourURL' ;
      $.ajax({
           type : 'POST' , 
           url : submitUrl , 
           data : formData , 
           success : function (data ){ alert ("Request successful") ;}
           error : function (jqXHR, status , errorthrown) { alert ("error Occured");}
        });

   } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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