我想隐藏
中的输入(new { htmlAttributes = new { @type = "number", @min = "1", @max="99", @placeholder = "Number of ???" } })
实际上触发了post方法时,我希望它随后隐藏输入。
Here's the form number input and button input.
我看过这个How to make @Html.EditorFor invisible?
// cshtml
@using (@Html.BeginForm("ConfirmOrder", "Home", FormMethod.Post))
{
@Html.EditorFor(model => model.Number, new { htmlAttributes = new { @type = "number", @min = "1", @max = "99", @placeholder = "Number of ???" } })
<input type="submit" value="Confirm" />
}
// cshtml.cs代码
public class DetailsModel : PageModel
{
private readonly ShopDashboard.Models.ShopDashboardContext _context;
public DetailsModel(ShopDashboard.Models.ShopDashboardContext context)
{
_context = context;
}
public int Number { get; set; }
public Order Order { get; set; }
public async Task<IActionResult> OnGetAsync(string id)
{
if (id == null)
{
return NotFound();
}
Order = await Task.Run(() => Post.GetOrders().FirstOrDefault(m => m.Id == id));
if (Order == null)
{
return NotFound();
}
return Page();
}
public async Task<IActionResult> OnPostAsync(string id, int Number = -1)
{
if (id == null)
{
return NotFound();
}
Order = await Task.Run(() => Post.method().FirstOrDefault(m => m.Id == id));
if (Order != null)
{
IActionResult result;
if (Number == -1)
{
result = new OkObjectResult(JsonConvert.DeserializeObject(Post.method().Content));
return result;
}
MediaTypeHeaderValue mediaTypeHeaderValue = new MediaTypeHeaderValue("application/pdf");
IRestResponse response = Post.method();
result = new ContentResult() { Content = response.Content };
if (response.IsSuccessful)
{
FileContentResult contentResult = new FileContentResult(response.RawBytes, mediaTypeHeaderValue.MediaType)
{
FileDownloadName = "???" + response.Headers
.Where(header => header.Name == "Content-Disposition").First()
.Value.ToString().Split("=")[1].Split("?").First() + ".pdf"
};
result = contentResult;
}
return result;
}
return Page();
}
答案 0 :(得分:0)
好的,所以我准备了一个简单的MVC
应用程序,以演示如何使用Hidden
元素来实现功能。该程序将接受简单的用户输入并将其发布到ActionResult
。收到隐藏值后,它将仅隐藏您的数字选择器。您可以根据需要定制程序。
您的Model
类如下:
namespace ExampleMVC
{
public class SampleViewModel
{
public string inputID { get; set; }
public string lastinputID { get; set; }
}
}
您的View
看起来像(索引):
@model ExampleMVC.SampleViewModel
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSS Includes -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="col-md-6 col-md-offset-3">
<h2>Input your number here</h2>
@using (Html.BeginForm("ConfirmOrder", "Home", FormMethod.Post))
{
<div class="form-group">
@if (Convert.ToInt32(Model.lastinputID) == 0)
{
@Html.EditorFor(model => model.inputID, new { htmlAttributes = new { @type = "number", @min = "1", @max = "99", @placeholder = "Number of ???" } })
}
else
{
<h3>Value recieved, hiding input</h3>
}
@Html.HiddenFor(model => model.lastinputID)
<br>
<br>
Last Value: @Model.lastinputID
</div>
<button type="submit" class="btn btn-success submit">Submit</button>
}
</div>
</div>
</body>
</html>
您的Controller
如下所示:
using System.Web.Mvc;
namespace ExampleMVC.Controllers
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
SampleViewModel sample = new SampleViewModel();
sample.lastinputID = "0";
return View(sample);
}
[HttpPost]
public ActionResult ConfirmOrder(SampleViewModel sample)
{
sample.lastinputID = sample.inputID;
//Do something here
return View("Index",sample);
}
}
}
希望这对您的问题有所帮助。