I currently have some unresolved problems. I hope you can help me I have a Model to initialize data as follows
public enum ContentType : int
{
[Display(Name = "CkEditor || Dữ liệu cơ bản")]
CkEditor = 0,
[Display(Name = "CodeMirror || Dữ liệu nâng cao")]
CodeMirror = 1,
}
public class StaticInformation : IAggregateRoot
{
public int Id { get; set; }
public string Name { get; set; }
public string Content { get; set; }
public bool Status { get; set; }
[MaxLength(10)]
public string Language { get; set; }
public bool Delete { get; set; }
public string Code { get; set; }
public ContentType ContentType { get; set; }
}
public class StaticInformationModel
{
public int Id { get; set; }
public string Name { get; set; }
[Display(Name = "Nội dung")]
public string Content { get; set; }
[Display(Name = "Sử dụng dữ liệu này")]
public bool Status { get; set; }
[MaxLength(10)]
public string Language { get; set; }
public bool Delete { get; set; }
public string Code { get; set; }
public ContentType ContentType { get; set; }
}
And here is the Controller to save Data
[HttpPost, ActionName("Create")]
[AuthorizePermission("Index")]
public async Task<ResponseModel> CreatePost(StaticInformationModel use)
{
try
{
if (ModelState.IsValid)
{
var data = new StaticInformation
{
Name = use.Name,
Content = use.Content,
Delete = false,
Status = use.Status,
Language = use.Language,
Code = use.Code,
ContentType = (ContentType)use.ContentType
};
if (!string.IsNullOrEmpty(use.Code))
{
var existedPost = await _iStaticInformationRepository.SingleOrDefaultAsync(true, m => m.Code == use.Code && m.Language == use.Language);
if (existedPost != null)
{
return new ResponseModel() { Output = 0, Message = "Mã đã tồn tại", Type = ResponseTypeMessage.Warning };
}
}
await _iStaticInformationRepository.AddAsync(data);
await _iStaticInformationRepository.CommitAsync();
if (data.Status)
{
CommonFunctions.GenModule(_iHostingEnvironment.WebRootPath, data.Content, ModuleType.StaticInformation, data.Id, data.Language, data.Code);
}
await AddLog(new LogModel
{
ObjectId = data.Id,
ActionTime = DateTime.Now,
Name = $"Thêm mới thông tin tĩnh \"{data.Name}\".",
Type = LogType.Create
});
return new ResponseModel() { Output = 1, Message = "Thêm mới thông tin tĩnh thành công ", Type = ResponseTypeMessage.Success, IsClosePopup = true };
}
return new ResponseModel() { Output = 0, Message = "Bạn chưa nhập đầy đủ thông tin", Type = ResponseTypeMessage.Warning };
}
catch (Exception ex)
{
_logger.LogError(LoggingEvents.GENERATE_ITEMS, "#Trong-[Log]{0}", ex);
}
return new ResponseModel() { Output = -1, Message = "Đã xảy ra lỗi, vui lòng F5 trình duyệt và thử lại", Type = ResponseTypeMessage.Danger, Status = false };
}
And here is View
<form form-edit asp-action="Create" accept-charset="utf-8" id="yourFormId">
<input type="hidden" asp-for="Language" />
@Html.AntiForgeryToken()
<input asp-for="Language" type="hidden" />
<div class="sumaryValidateForm" asp-validation-summary="All"></div>
<div class="modal-header">
<h4 class="modal-title"><i class="material-icons">edit</i> THÊM MỚI THÔNG TIN TĨNH - <span class="text-danger">@PT.Shared.ListData.ListLanguage.FirstOrDefault(x => x.Id == Model.Language)?.Name</span></h4>
</div>
<button title="Đóng cửa sổ" type="button" class="close btnclose-t" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<div class="modal-body">
<div class="divmodal">
<div class="form-group">
<label class="form-label" asp-for="Name"></label> <strong class="text-danger">*</strong> <span asp-validation-for="Name"></span>
<div class="controls">
<input type="text" class="form-control input-sm form-control-t" asp-for="Name">
</div>
</div>
<div class="form-group">
<label class="form-label" asp-for="Code"></label> <strong class="text-danger">*</strong> <span asp-validation-for="Code"></span>
<div class="controls">
<input type="text" class="form-control input-sm form-control-t" asp-for="Code" maxlength="20">
</div>
</div>
<div class="form-group">
<label class="form-label" asp-for="Content"></label> <span asp-validation-for="Content"></span>
<div class="controls">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="colorRadio" id="inlineRadio1" value="0" asp-for="ContentType" checked />
<label class="form-check-label" for="inlineRadio1">CkEditor</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="colorRadio" id="inlineRadio2" value="1" asp-for="ContentType" />
<label class="form-check-label" for="inlineRadio2">CodeMirror</label>
</div>
<input type="hidden" id="contentTypeValue" name="ContentType" value="0" />
<div class="CkEditor selectt">
<textarea id="ricktextContent" seo-page-content class="form-control form-control-t" rows="3" placeholder="Mô tả" asp-for="Content"></textarea>
</div>
<div class="CodeMirror selectt">
<textarea id="ricktextContentDiv" class="form-control form-control-t" rows="3" placeholder="Mô tả" asp-for="Content"></textarea>
</div>
</div>
</div>
<div class="pt-10"></div>
<div class="form-group">
<input type="checkbox" asp-for="Status" class="filled-in chk-col-green" />
<label for="Status" asp-for="Status"></label>
</div>
</div>
</div>
<div class="modal-footer" style="text-align:center;">
<a class="btn btn-icon btn-default waves-effect" data-dismiss="modal"><i class="material-icons">reply</i><span>Đóng</span></a>
<a button-submit data-language="@Model.Language" data-loading-text="<i class='material-icons icon-spin'>autorenew</i> <span>Lưu thay đổi</span>" class="btn btn-icon bg-indigo waves-effect"><i class="material-icons">save</i> <span>Lưu thay đổi</span></a>
</div>
</form>
Currently, I am using 2 textareas to save Content: CkEditor and CodeMirror. I use EnumType to divide, for example, if the user chooses CkEditor, the Content will be saved and has type = 0. If saved with CodeMirror, the Content will be saved. and has type = 1, but currently it can only be saved with CkEditor and CodeMirror will return null, how can I save with both?
I have tried a few ways but none of them work. If you have any suggestions, please let me know. Currently when saving, it can only be saved with CkEditor and CodeMirror will be null. Currently I just want if the user selects CkEditor, it will save the Content and have a ContentType of 0 and Codemirror will save the Content and have a ContentType of 1 and most importantly, it can save the Content. I sincerely thank
I can replicate your issue and it's due to using the same name for the 2 textareas. Form Submit uses the name attribute for binding value. If we use the same name in 2 different form elements, we would get your issue.
So the easiest solution is to add another property for CodeMonitor
textarea. Add a new property named CodeMonitorContent
and using asp-for="CodeMonitorContent"
to replace the asp-for="Content"
in the second textarea.
And you can also change string
to string[]
like public string[] Content { get; set; }
. This is easy as well.