1

I am new on asp.net and i have a problem with forms. My action function does not catch data. All of my data are null and I can not fix it.

Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace Blog.Models
{
public class RegisterViewModel
{
    [Required]
    public string UserName { get; set; }
    [Required]
    [DataType(DataType.Password)]
    public string Password { get; set; }
    [Required]
    [DataType(DataType.Password)]
    public string ConfirmPassword { get; set; }
    [Required]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
    [Required]
    public string FullName { get; set; }
    [Required]
    [DataType(DataType.Date)]
    public DateTime BirthDate { get; set; }
}
}

My cshtml file

 <form asp-action="Register" method="post">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="UserName" class="control-label"></label>
            <input asp-for="UserName" class="form-control" />
            <span asp-validation-for="UserName" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Password" class="control-label"></label>
            <input asp-for="Password" type="password" class="form-control" />
            <span asp-validation-for="Password" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="ConfirmPassword" class="control-label"></label>
            <input asp-for="ConfirmPassword" type="password" class="form-control" />
            <span asp-validation-for="ConfirmPassword" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Email" class="control-label"></label>
            <input asp-for="Email" class="form-control" />
            <span asp-validation-for="Email" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="FullName" class="control-label"></label>
            <input asp-for="FullName" class="form-control" />
            <span asp-validation-for="FullName" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="BirthDate" class="control-label"></label>
            <input asp-for="BirthDate" type="date" class="form-control" />
            <span asp-validation-for="BirthDate" class="text-danger"></span>
        </div>
        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </form>

My Startup.cs

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder();
        builder.SetBasePath(env.ContentRootPath);
        builder.AddJsonFile("appsettings.json");
        config = builder.Build();
    }
    public void ConfigureServices(IServiceCollection services)
    {

        services.AddDbContext<AppDbContext>(options => options.UseSqlite("Data Source=blog.db"));

        services.AddMvc();

        services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");

        services.AddIdentity<MyIdentityUser, IdentityRole>()
            .AddEntityFrameworkStores<AppDbContext>()
            .AddDefaultTokenProviders();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseMvc();
        app.UseMvcWithDefaultRoute();
        //app.UseMvc(routes =>
        //{
        //    routes.MapRoute(
        //        name: "default",
        //        template: "{controller=Account}/{action=Index}/{id?}");
        //});
        app.UseAuthentication();
        app.UseDeveloperExceptionPage();
        app.UseStaticFiles();
        //app.UseIdentity();
    }

and my controller class

public class AccountController : Controller
{
    private readonly UserManager<MyIdentityUser> userManager;
    private readonly SignInManager<MyIdentityUser> loginManager;
    private readonly RoleManager<IdentityRole> roleManager;


    public AccountController(UserManager<MyIdentityUser> userManager,
       SignInManager<MyIdentityUser> loginManager,
       RoleManager<IdentityRole> roleManager)
    {
        this.userManager = userManager;
        this.loginManager = loginManager;
        this.roleManager = roleManager;
    }

    public IActionResult Index()
    {
        return View();
    }

    public IActionResult Register()
    {
        return View();
    }

    [HttpPost]
    public IActionResult Register(RegisterViewModel obj)
    {
        // fields of obj is null here(Username,Password...)
        if (ModelState.IsValid)
        {
            somethings
        }
        return View(obj);
    }

Following code is source of page

<h2>Register</h2>

<h4>RegisterViewModel</h4>
<hr />
<div class="row">
<div class="col-md-4">
    <form asp-action="Register" method="post">
        <div asp-validation-summary="ModelOnly" class="text-danger">      </div>
        <div class="form-group">
            <label asp-for="UserName" class="control-label"></label>
            <input asp-for="UserName" class="form-control" />
            <span asp-validation-for="UserName" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Password" class="control-label"></label>
            <input asp-for="Password" type="password" class="form-control" />
            <span asp-validation-for="Password" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="ConfirmPassword" class="control-label"></label>
            <input asp-for="ConfirmPassword" type="password" class="form-control" />
            <span asp-validation-for="ConfirmPassword" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Email" class="control-label"></label>
            <input asp-for="Email" class="form-control" />
            <span asp-validation-for="Email" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="FullName" class="control-label"></label>
            <input asp-for="FullName" class="form-control" />
            <span asp-validation-for="FullName" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="BirthDate" class="control-label"></label>
            <input asp-for="BirthDate" type="date" class="form-control" />
            <span asp-validation-for="BirthDate" class="text-danger"></span>
        </div>
        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </form>
</div>
</div>

<div>
<a asp-action="Index">Back to List</a>
</div>
7
  • Possible duplicate of stackoverflow.com/questions/40172051/… Commented Sep 1, 2018 at 12:31
  • @DmytroMukalov I have already seen that post but if I am not wrong. My situation doesn't fit that. Commented Sep 1, 2018 at 12:55
  • what name is given to <input asp-for="UserName" class="form-control" /> in page source ? check in browser Commented Sep 1, 2018 at 13:21
  • I added source of page just now. Commented Sep 1, 2018 at 18:06
  • 1
    The asp-for (more specifically, the Input Tag Helper) handles setting name for you. You need to make sure the MVC Tag Helpers are registered in e.g. your _ViewImports.cshtml file for it to work. Commented Sep 1, 2018 at 18:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.