simpsonment1
simpsonment1

Reputation: 49

Saving post from textarea to database in Razor pages WebApplication

I'm trying to build a social network on a ASP.NET Core Web Application in Visual Studio. There is membership and a wall, where members can make posts. I'm using Tinymce for a textarea with editing tools, and I'm supposed to save the text in the textarea in HTML form to my local Visual Studio database, along with the UserId of the connected user who is posting.

When I run the application and try to post, neither of these seem to work. I'm getting a null entry in the database for both the Text and the UserId.

While UserManager works properly on the .cshtml file and does return the UserId, I can't get it to work on the .cs file

Related code so far, of the .cshtml file:

    @using Microsoft.AspNetCore.Identity
    @inject UserManager<ApplicationUser> UserManager
    @model IndexModel
    @{
        ViewData["Title"] = "Home page";
    }
    <form asp-route-returnUrl="@Model.ReturnUrl" method="post" enctype="multipart/form-data" asp-controller="UploadFiles" asp-action="Index">
        <div class="form-group">
            <p>You are connected and can post as @UserManager.GetUserId(User)</p>
            <textarea name="txt" id="mytextarea" rows="2" cols="80" asp-for="Posts.Text" >
            </textarea>
        </div>
        <br />
        <div class="form-group">
            <input type="submit" value="Post" class="btn btn-default" />
        </div>
    </form>

Related code of the .cs file:

    private readonly proj.Data.ApplicationDbContext _context;

    public IndexModel(proj.Data.ApplicationDbContext context)
    {
        _context = context;
    }    
    public Models.Posts Posts { get; set; }
    public async Task<IActionResult> OnPostAsync(string returnUrl = null)
    {
        UserManager<Data.ApplicationUser> UserManager;
        var post = new Models.Posts {UserId = UserManager.GetUserId(User), Text = Posts.Text };
        _context.Posts.Add(post);
        await _context.SaveChangesAsync();

        return Page();
    }

I get an "unassigned local variable" error for UserManager. Can you please help me get the content of the textarea correctly, and define UserManager properly?

Upvotes: 0

Views: 1172

Answers (2)

simpsonment1
simpsonment1

Reputation: 49

I figured out the textarea problem. The line should be like this instead:

<textarea asp-for="Posts.Text" id="mytextarea" rows="2" cols="80" class="form-control"></textarea>

I forgot to include class="form-control" and name="txt" was somehow causing a problem and needed to be omitted.

Upvotes: 0

D-Shih
D-Shih

Reputation: 46219

You seem to have used Depend injection so you can try constructor injection

private readonly proj.Data.ApplicationDbContext _context;

private UserManager<Data.ApplicationUser> _userMannger;

public IndexModel(proj.Data.ApplicationDbContext context, 
   UserManager<Data.ApplicationUser> user)
{
   _userMannger= user;
    _context = context;
}   
public Models.Posts Posts { get; set; }
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
    var post = new Models.Posts {UserId = _userMannger.GetUserId(User), Text = Posts.Text };
    _context.Posts.Add(post);
    await _context.SaveChangesAsync();

    return Page();
}

Upvotes: 1

Related Questions