-4

I had created an HTML form using c# string builder using reference: https://stackoverflow.com/a/37757601/7961948

and in dotnet, we could submit this form using

Response.Write(formPostText);

But right now I am on DOTNET CORE 2.1 and DOTNET CORE does not support dotnet function so there is any another way to submit a form

sample code: working on dotnet version

     var formPostText = @"<html><body><div>
    <form method=""POST"" action=""OtherLogin.aspx"" name=""frm2Post"">
      <input type=""hidden"" name=""field1"" value=""" + TextBox1.Text + @""" /> 
      <input type=""hidden"" name=""field2"" value=""" + TextBox2.Text + @""" /> 
    </form></div><script type=""text/javascript"">document.frm2Post.submit();</script></body></html>
    ";

Response.Write(formPostText);
Amit Singh Rawat
  • 559
  • 1
  • 9
  • 27
  • 2
    Response.Write does not submit a form. Assuming you're in an ASP.NET app it would just write some text to the response object, which would presumably be rendered in a browser. So your question doesn't make a whole lot of sense. – mason Jun 20 '19 at 13:26
  • 1
    If your goal is to create an HTML form that users fill out, and allow them to submit that form to your server, and capture the response, then you should probably check out [ASP.NET Core MVC](https://learn.microsoft.com/en-us/aspnet/core/mvc/overview?view=aspnetcore-2.2) for which there are some great tutorials. – mason Jun 20 '19 at 13:29
  • Do you mean you want to write some data to the client? "Submit" and "form" seem like they might be red herrings. – ProgrammingLlama Jun 20 '19 at 13:30
  • You need to be explicit about what's not working. All you say is "does not support dotnet version" but you don't say what it is that's failing. Be clear with your question. Keep in mind we have no idea what you're doing. We're not sitting at your computer. It's up to you to adequately explain your situation. – mason Jun 20 '19 at 13:41

2 Answers2

0

It's not clear if you're using MVC or Web API, but in either case, it would be a good idea to look at the following resources when considering how to use ModelState validation to validate form input:

The approaches are generally the same but with some variations.

Regarding your HTML stored in formPostText, is there any reason you are building your form using a string? I would recommend using a view as doing so will better separate your presentation layer from your logic layer. If you must generate the raw HTML content, you can do something like this:

private string GetViewHtml(FormViewModel model)
{
    return this.RenderView("/Views/Shared/FormView.cshtml", model);
}

In this example, which was built for MVC 5, RenderView is an extension method of the controller:

public static class ControllerExtensions
{
    public static string RenderView(this System.Web.Mvc.Controller controller, string viewName, object model)
    {
        return RenderView(controller, viewName, new ViewDataDictionary(model));
    }

    public static string RenderView(this System.Web.Mvc.Controller controller, string viewName, ViewDataDictionary viewData)
    {
        var controllerContext = controller.ControllerContext;

        var viewResult = ViewEngines.Engines.FindView(controllerContext, viewName, null);

        StringWriter stringWriter;

        using (stringWriter = new StringWriter())
        {
            var viewContext = new ViewContext(
                controllerContext,
                viewResult.View,
                viewData,
                controllerContext.Controller.TempData,
                stringWriter);

            viewResult.View.Render(viewContext, stringWriter);
            viewResult.ViewEngine.ReleaseView(controllerContext, viewResult.View);
        }

        return stringWriter.ToString();
    }
}
user1477388
  • 20,790
  • 32
  • 144
  • 264
0

Dotnet core support

var s = "some html / html form";

HttpContext.Response.WriteAsync(s.ToString());

Find refrence : https://gist.github.com/priore/7163408

Is there an equivalent to "HttpContext.Response.Write" in Asp.Net Core 2?

Amit Singh Rawat
  • 559
  • 1
  • 9
  • 27