Modify value before I display it in the view? - c#

I have a feeds table
Id Message Created
1 aaa 2016-02-25 12:18:51
2 bbb 2016-02-24 12:18:51
3 ccc 2015-02-25 12:18:51
I can get all the values like this
public ActionResult Index()
{
using (var db = new ApplicationDbContext())
{
var feeds = db.Feeds.ToList();
return View(feeds);
}
}
I have created a class that change date from datetime to e.g "1 day ago". And this works fine
var myTime = DateExtension.TimeAgo(DateTime.Parse("2016-02-25 12:18:51"));
I want to modify all the dates in feed with help of DateExtension.TimeAgo before I return in to the view. How can I do that?

try this:
return View(
feeds.Select(x=>
new Feed{
Id=x.Id,
Message=x.Message,
Created=DateExtension.TimeAgo(DateTime.Parse(x.Created))
}).toList());
where Feed your class, with the same fields

Use FeedDisplayModel class in your view
public class Class1TestController
{
public ActionResult Index()
{
using (var db = new ApplicationDbContext())
{
var feeds = db.Feeds.Select(itm=>new FeedDisplayModel(itm)).ToList();
return View(feeds);
}
}
}
class Feed
{
public DateTime Created { get; set; }
public int Id { get; set;}
public string Message { get; set;}
}
class FeedDisplayModel : Feed
{
public string Ago { get { return Created.TimeAgo(); } }
public FeedDisplayModel(Feed itm){
this.Created=itm.Created;
this.Id=itm.Id;
this.Message=itm.Message;
}
}
public static class DateExtension
{
public static string TimeAgo(this DateTime dt)
{
return "your implementation of ";
}
}

Related

How to fetch data from a class with two list inside?

I have a class with 2 list inside and don't know how to fetch through all 2 list to get all data,
thank you for reading my question.
my MODEL
namespace BaoCao.Models
{
public class ProductPage
{
public List<PRODUCT> listproduct { get; set; }
public List<TYPE_PRODUCTS> listtype_product { get; set; }
public static ProductPage getAll()
{
DB db = new DB();
ProductPage a = new ProductPage();
a.listproduct = db.PRODUCTS.ToList();
a.listtype_product = db.TYPE_PRODUCTS.ToList();
return a;
}
ProductPage()
{
}
}
}
MY VIEW
namespace BaoCao.Controllers
{
public class ProductController : Controller
{
// GET: Product
public ActionResult Product()
{
var dssp = ProductPage.getAll();
return View(dssp);
}
}
}
Product.cshtml
#model BaoCao.Models.ProductPage
#foreach (var product in Model.listproduct) {
...
}
The details are here.

Parse FHIR Patient response to json

I'm new to .Net and FHIR. I followed a few tutorial to understand how FHIR API works. I need to create an application that use only GET request to get data from server. Below, I am trying to make a request to retrieve a patient by ID in PatientRepository class. However, when I test it with Postman, it doesn't return any response. How should I change my code? Many thanks
Model:
public class Patient : Hl7.Fhir.Model.Patient
{
public string name { get; set; }
public string birthday { get; set; }
}
public class PatientList
{
public List<Patient> Patients { get; set; }
}
Controller:
public class PatientController : Controller
{
private readonly IPatientRepository _patientRepository;
public PatientController(IPatientRepository patientRepository)
{
_patientRepository = patientRepository;
}
[HttpGet]
[Route("api/GetPatientById/{id}")]
public IActionResult getPatientById(long id)
{
var model = _patientRepository.GetPatientById(id);
if (model == null)
return NotFound();
return Ok(model);
}
}
}
PatientRepository:
public class PatientRepository : IPatientRepository
{
public async Task<Patient> GetPatientById(long id)
{
var client = new FhirClient("https://fhir.****.***/hapi-fhir-jpaserver/fhir/");
client.Timeout = (60 * 100);
client.PreferredFormat = ResourceFormat.Json;
var pat = client.Read<Patient>("Patient/1");
var parser = new FhirJsonParser();
return new Patient
{
birthday = pat.birthday,
}
}
}

How can i save multiple collections with some common data to database?

I'm trying to save collections with part common data for all and different part for each collection. I wrote hard coded way to save one collection and I don't know how I can do it for multiple collections. So this is what I have:
Controller:
[HttpPost]
public ActionResult MakeComplaint(CommonData model)
{
if (ModelState.IsValid)
{
model.CreateComplaint();
return RedirectToAction("Index");
}
}
Model:
public class CommonData
{
public string Data1 { get; set; }
public string Data2 { get; set; }
public List<DifferentData> Differents { get; set; }
SecondModel DataContainer()
{ //help needed here
SecondModel tmp = new SecondModel();
tmp.ID = Differents[0].ID; //how can I change it to save more collections?
tmp.Name = Differents[0].Name;
tmp.Data1 = Data1;
tmp.Data2 = Data2;
return tmp;
}
public int CreateComplaint()
{
var complaint = DataContainer();
db.AddComplaint(complaint);
db.Save();
}
}
public class DifferentData
{
public int ID { get; set; }
public string Name { get; set; }
}
How should my DataContainer() look in order to handle Differents[1] and more?
EDIT:
My DataContainer() is a list of objects now, so 1 problem solved
List<SecondModel> DataContainer()
{
var listOfComplaints = new List<SecondModel>;
for(int i = 0; i<Differents.Count; i++){
SecondModel tmp = new SecondModel();
tmp.ID = Differents[i].ID;
tmp.Name = Differents[i].Name;
tmp.Data1 = Data1;
tmp.Data2 = Data2;
listOfComplaints.Add(tmp)
}
return listOfComplanints;
}
EDIT2:
Working solution:
public void AddComplaint(List<SecondModel> model){
db.SecondModels.InsertAllOnSubmit(model) //saving multiple objects
}
Usually you get new Models from the page. Not as a DataContainer but as a SecondModel or another specific model. Your DataContainer and nested classes in CommonData is probably adding confusion. You can actually remove all of that, and use the code you have in AddComplaint directly from your controller like this:
[HttpPost]
public ActionResult MakeComplaint(SecondModel model)
{
if (ModelState.IsValid)
{
db.SecondModels.InsertOnSubmit(model)
return RedirectToAction("Index");
}
}
and just do something similar for another model:
[HttpPost]
public ActionResult MakeSomethingElse(ThirdModel model)
{
if (ModelState.IsValid)
{
db.ThirdModels.InsertOnSubmit(model)
return RedirectToAction("Index");
}
}

How to get JSON result into List

I have my own code here and already get the JSON result then I haven't idea to move the JSON result into the interface (IEntities function) list below.
class GetCategory : IEntities
{
private JsonHandle _jsonhandle;
private string _ocategory;
public async void TaskCategory()
{
_jsonhandle = new JsonHandle();
_jsonhandle.StrAPI = "http://api.nytimes.com/svc/books/v3/lists/names.json?api-key=7bb034b7693d6f9753b2f68e00b98c78%3A16%3A73599437";
var client = new HttpClient();
Task<string> datatask = client.GetStringAsync(_jsonhandle.StrAPI);
try
{
var JsonRead = await datatask;
JObject oCategory = JObject.Parse(JsonRead);
List<JToken> results = oCategory["results"].Children().ToList();
//serialize JSON results into .NET objects
List<object> dtCategory = new List<object>();
foreach (JToken result in results)
{
object _dtcategory = JsonConvert.DeserializeObject<object>(result.ToString());
dtCategory.Add(_dtcategory);
var listname = result["list_name"];
}
}
catch (Exception error)
{
Console.WriteLine("AW!" + error.StackTrace);
}
public List<object> BookCategory()
{
}
}
In the last function that in IEntities interface, I need to put my JSON result in interface List<object>.
When working with JSON, the first thing to do is creating a model object. In order to this, either you should analyze JSON output manually, or you can generate the model automatically by going to the following link and pasting either the JSON your are going to use or the service link;
json2csharp.com
I've just used your API link and the output generated is;
public class Result
{
public string list_name { get; set; }
public string display_name { get; set; }
public string list_name_encoded { get; set; }
public string oldest_published_date { get; set; }
public string newest_published_date { get; set; }
public string updated { get; set; }
}
public class RootObject
{
public string status { get; set; }
public string copyright { get; set; }
public int num_results { get; set; }
public List<Result> results { get; set; }
}
This will be our model.
Secondly, as far as I can see, what you want to do is only to get the result list, thus, as you deserialize the JSON output, you are going to use the Model.Result and move them into a list.
Then, to get the response, a private method can be used which that returns an async Task<string>, and on the BookCategory() method, you can get the Results and deserialize JSON and initialize a list based on your JSON object model;
public List<Model.Result> BookCategory()
{
List<Model.Result> list = new List<Model.Result>();
var model = JsonConvert.DeserializeObject<Model.RootObject>(TaskCategory().Result);
list = model.results;
return list;
}
Deserializing JSON is as simply as below;
var model = JsonConvert.DeserializeObject<Model.RootObject>(TaskCategory().Result);
Model.cs
using System.Collections.Generic;
namespace SO1
{
public class Model
{
public class Result
{
public string list_name { get; set; }
public string display_name { get; set; }
public string list_name_encoded { get; set; }
public string oldest_published_date { get; set; }
public string newest_published_date { get; set; }
public string updated { get; set; }
}
public class RootObject
{
public string status { get; set; }
public string copyright { get; set; }
public int num_results { get; set; }
public List<Result> results { get; set; }
}
}
}
GetCategory.cs
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Net.Http;
using System;
namespace SO1
{
public class GetCategory : IEntities
{
private String BaseUri;
public GetCategory(string BaseUri)
{
this.BaseUri = BaseUri;
}
private async Task<string> TaskCategory()
{
var httpClient = new HttpClient();
var parameters = new Dictionary<string, string>();
parameters["text"] = "text";
var response = await httpClient.GetStringAsync(BaseUri);
return response;
}
public List<Model.Result> BookCategory()
{
List<Model.Result> list = new List<Model.Result>();
var model = JsonConvert.DeserializeObject<Model.RootObject>(TaskCategory().Result);
list = model.results;
return list;
}
}
}
IEntities
using System.Collections.Generic;
namespace SO1
{
public interface IEntities
{
List<Model.Result> BookCategory();
}
}
Program.cs
using System;
using System.Collections.Generic;
namespace SO1
{
class Program
{
static void Main(string[] args)
{
string BaseUri = "http://api.nytimes.com/svc/books/v3/lists/names.json?api-key=7bb034b7693d6f9753b2f68e00b98c78%3A16%3A73599437";
IEntities entity = new GetCategory(BaseUri);
List<Model.Result> listBookCategory = new List<Model.Result>();
listBookCategory = entity.BookCategory();
foreach (Model.Result r in listBookCategory)
{
Console.WriteLine();
Console.WriteLine("...List Name : " + r.list_name);
Console.WriteLine("...Display Name : " + r.display_name);
Console.WriteLine("...List Name Encoded : " + r.list_name_encoded);
Console.WriteLine("...Oldest Published Date : " + r.oldest_published_date);
Console.WriteLine("...Oldest Published Date : " + r.newest_published_date);
Console.WriteLine("...Updated : " + r.updated);
Console.WriteLine();
}
}
}
}
Output (only first three results printed)
...List Name : Combined Print and E-Book Fiction
...Display Name : Combined Print & E-Book Fiction
...List Name Encoded : combined-print-and-e-book-fiction
...Oldest Published Date : 2011-02-13
...Oldest Published Date : 2015-12-27
...Updated : WEEKLY
...List Name : Combined Print and E-Book Nonfiction
...Display Name : Combined Print & E-Book Nonfiction
...List Name Encoded : combined-print-and-e-book-nonfiction
...Oldest Published Date : 2011-02-13
...Oldest Published Date : 2015-12-27
...Updated : WEEKLY
...List Name : Hardcover Fiction
...Display Name : Hardcover Fiction
...List Name Encoded : hardcover-fiction
...Oldest Published Date : 2008-06-08
...Oldest Published Date : 2015-12-27
...Updated : WEEKLY

.net create object instance from abstract class

I have the following abstract class:
public abstract class TemplateBase
{
public abstract string TemplateName { get; }
public string RuntimeTypeName { get { return GetType().FullName; } }
public abstract List<AreaContainer> TemplateAreas { get; }
}
then these 2 inherited classes:
public class SingleColumnTemplate : TemplateBase
{
public override string TemplateName { get { return "Single column"; } }
public AreaContainer CenterColumn { get; private set; }
public SingleColumnTemplate()
{
this.CenterColumn = new AreaContainer("Middle");
}
private List<AreaContainer> templateAreas;
public override List<AreaContainer> TemplateAreas
{
get
{
if (this.templateAreas == null)
{
this.templateAreas = new List<AreaContainer>() { this.CenterColumn };
}
return this.templateAreas;
}
}
}
and
public class TwoColumnTemplate : TemplateBase
{
public override string TemplateName { get { return "Two column"; } }
public AreaContainer LeftColumn { get; private set; }
public AreaContainer RightColumn { get; private set; }
public TwoColumnTemplate()
{
LeftColumn = new AreaContainer("Left");
RightColumn = new AreaContainer("Right");
}
private List<AreaContainer> templateAreas;
public override List<AreaContainer> TemplateAreas
{
get
{
if (this.templateAreas == null)
{
this.templateAreas = new List<AreaContainer>() { this.LeftColumn, this.RightColumn };
}
return this.templateAreas;
}
}
}
I also have this class that is my model for editing:
public class ContentPage
{
public virtual int ContentPageId { get; set; }
public virtual string Title { get; set; }
public TemplateBase Template { get; set; }
}
Question:
for my ActionResults I have the following:
[HttpGet]
public ActionResult Edit()
{
var row = new ContentPage();
var template = new TwoColumnTemplate();
// Areas
HtmlArea html_left = new HtmlArea();
html_left.HtmlContent = "left area html content";
HtmlArea html_right = new HtmlArea();
html_right.HtmlContent = "right area html content";
template.LeftColumn.Areas.Add(html_left);
template.RightColumn.Areas.Add(html_right);
row.Template = template;
return View(row);
}
[HttpPost]
[ValidateInput(false)]
public ActionResult Edit(ContentPage row)
{
// Here i could loop through List -TemplateAreas and save each template Area to Db. I guess that would work
return this.View(row);
}
Question:
For HttpGet- how would I load row Template from the database? since it could be SingleColumnClass or TwoColumnClass.
how would my ViewModel look like to solve this?
thanks
You can write your own Model Binder that is responsible for binding TemplateBase. You will still need to have a way of knowing (in the model binder) which type you will be using a runtime, but you can always delegate that to a factory or service locator of some sort. I did a quick google search and here is a blog post I found that gives you some information for making a model binder for a similar scenario:
http://weblogs.asp.net/bhaskarghosh/archive/2009/07/08/7143564.aspx
EDIT: The blog leaves out how you tell MVC about your model binder. When the application starts, you can add your model binder to System.Web.Mvc.ModelBinders.Binders
HTH
You need to know the template type in you controller, so you can pass a parameter from the view to the controller, indicating the type (SingleColumn or TwoColumn). You could do this witn a Enum:
public enum TemplateType
{
SingleColumn,
TwoColumn
}
[HttpGet]
public ActionResult Edit(TemplateType templateType)
{
var row = new ContentPage();
TemplateBase template;
if (templateType == TemplateType.SingleColumn)
{
template = new SingleColumnTemplate();
}
else
{
template = new TwoColumnTemplate();
}
...
return View(row);
}
When you create the action link from your view you can specify:
<%= Html.ActionLink("Edit",
"Edit",
"YouController",
new
{
// singlecolumn or twocolumn
// depending on your concrete view
TemplateType = TemplateType.xxx
},
null);
I wonder if you could do something like this?
[HttpGet]
public ActionResult Edit(TemplateType templateType)
{
var row = new ContentPage();
TemplateBase template = (TemplateBase)Activator.CreateInstance(templateType);
...
return View(row);
}
templateType would have to be the exact name of your inherited classes (you can ignore case)

Categories

Resources