0

I want to hide the input which is in a

(new { htmlAttributes = new { @type = "number", @min = "1", @max="99", @placeholder = "Number of ???" } })

when the post method is actually triggered, I want it to hide the input afterward.

Here's the form number input and button input.

I've seen this 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 code

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();
}
  • Show how you're posting the form. – Jabberwocky Jul 03 '19 at 16:14
  • you mean an image or ..? – Luc Desforges Jul 03 '19 at 16:28
  • I mean the code. – Jabberwocky Jul 03 '19 at 16:29
  • it's the //cshtml.cs code (mvc razor) – Luc Desforges Jul 03 '19 at 16:32
  • That's really not the way to do it. – Jabberwocky Jul 03 '19 at 16:35
  • I've updated the code, and can you explain? – Luc Desforges Jul 03 '19 at 16:41
  • You have to read about MVC. MVC stands for Model View Controller. Your controller is in a cshtml.cs, that's your task. That's non standard and basically ditches everything MVC stands for. Post the form with either a submit button (which binds automatically to your controller and your action) or with js. When this is a success, retrieve the success response and hide the button with js. All in all, you have to read about MVC – Jabberwocky Jul 03 '19 at 16:44
  • I know what you mean can you show me , how to link my button () to a controller? (btw I understand that what you are saying is really important, but I don't think it relates to the question) – Luc Desforges Jul 03 '19 at 16:47
  • https://stackoverflow.com/questions/14124324/submitting-form-and-pass-data-to-controller-method-of-type-filestreamresult – Jabberwocky Jul 03 '19 at 16:48
  • If you're using MVC, then you're probably using jquery, so you can do this with jquery (though it wasn't explicitly in the question): `$("form").on("submit", function() { $("#Number").hide(); });` – freedomn-m Jul 03 '19 at 16:49
  • the one who ask me to do this doesn't want to see any jquery is there another way? (is it in the link?) – Luc Desforges Jul 03 '19 at 16:51
  • While @Jabberwocky approach would normally be the way to go, there's 2 issues: 1) waiting to retrieve the response would generally be too late (but might be ok). 2) your're posting to `target=_blank` and returning a `FileContentResult`, so you won't be able to handle anything on the success (as it goes to a new tab and is a file). – freedomn-m Jul 03 '19 at 16:51
  • No jquery - does that also mean no javascript? If so, you're out of luck as described above: you're not POSTing back to the same page. – freedomn-m Jul 03 '19 at 16:52
  • You'd have to change how this works - eg POST, get a new form response (with the input hidden) then trigger a new window/meta-refresh(?) to get the document itself. – freedomn-m Jul 03 '19 at 16:54
  • Can you explain with an exemple? (or a link) – Luc Desforges Jul 03 '19 at 16:57
  • if I post back to the same page(not "_blank") would it be okay? (I think I can) – Luc Desforges Jul 03 '19 at 17:04
  • @LucDesforges So basically from my understanding, you want to retain the previous value of what was submitted via the form to the controller? For example, if I submitted 5, then that value would be posted to the Controller and after the postback, the same form would open but the value 5 would be stored in some hidden field? – Rahul Sharma Jul 03 '19 at 17:58
  • Most of it : yes. But I just want to hide the input (let's call it the number selector) but not the button it may or may not be stored I just want to hide it after it has been handled by the controller – Luc Desforges Jul 03 '19 at 18:07
  • I don't really care about about the condition to do it , I just want the number selector to be hidden or nothidden when something triggers a condition – Luc Desforges Jul 03 '19 at 18:11
  • @LucDesforges What is the number selector? Is it a text box? What do you want to show after you have posted your value to the controller ? Can you show your `View` code? – Rahul Sharma Jul 03 '19 at 18:23
  • Ive edited the cshtml.cs code maybe that can helps you? – Luc Desforges Jul 03 '19 at 18:39
  • @LucDesforges Are you using `MVC` architecture or `MVVM` architecture? – Rahul Sharma Jul 03 '19 at 18:40
  • number selector is: Html.EditorFor(model => model.Number, new { htmlAttributes = new { type = "number", min = "1", max = "99", placeholder = "Number of ???" } }) – Luc Desforges Jul 03 '19 at 18:41
  • thank you very much – Luc Desforges Jul 03 '19 at 18:43

0 Answers0