您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在C# MVC框架中实现用户反馈功能,可以通过以下步骤来完成:
public class FeedbackModel
{
public string Name { get; set; }
public string Email { get; set; }
public string Comment { get; set; }
public string Rating { get; set; }
}
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("api/[controller]")]
public class FeedbackController : ControllerBase
{
private readonly IFeedbackService _feedbackService;
public FeedbackController(IFeedbackService feedbackService)
{
_feedbackService = feedbackService;
}
[HttpPost]
public async Task<IActionResult> SubmitFeedback([FromBody] FeedbackModel feedback)
{
if (ModelState.IsValid)
{
await _feedbackService.SaveFeedbackAsync(feedback);
return Ok();
}
return BadRequest(ModelState);
}
}
using System.Threading.Tasks;
public interface IFeedbackService
{
Task SaveFeedbackAsync(FeedbackModel feedback);
}
public class FeedbackService : IFeedbackService
{
private readonly ApplicationDbContext _context;
public FeedbackService(ApplicationDbContext context)
{
_context = context;
}
public async Task SaveFeedbackAsync(FeedbackModel feedback)
{
var feedbackEntity = new FeedbackEntity
{
Name = feedback.Name,
Email = feedback.Email,
Comment = feedback.Comment,
Rating = feedback.Rating,
CreatedAt = DateTime.Now
};
_context.Feedbacks.Add(feedbackEntity);
await _context.SaveChangesAsync();
}
}
using Microsoft.AspNetCore.Identity;
public class FeedbackEntity
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Comment { get; set; }
public int Rating { get; set; }
public DateTime CreatedAt { get; set; }
}
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<FeedbackEntity> Feedbacks { get; set; }
}
@model FeedbackModel
<!DOCTYPE html>
<html>
<head>
<title>Submit Feedback</title>
</head>
<body>
<h1>Submit Your Feedback</h1>
<form asp-action="SubmitFeedback">
<label asp-for="Name"></label>
<input asp-for="Name" type="text" required />
<br />
<label asp-for="Email"></label>
<input asp-for="Email" type="email" required />
<br />
<label asp-for="Comment"></label>
<textarea asp-for="Comment" required></textarea>
<br />
<label asp-for="Rating"></label>
<select asp-for="Rating" required>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<br />
<button type="submit">Submit Feedback</button>
</form>
</body>
</html>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ...其他配置...
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
现在,用户可以通过访问Feedback视图并提交表单来提交反馈信息,这些信息将被保存到数据库中。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。