samedi 9 mai 2015

jquery onload select days

I'm using this plugin to select multiple week days from the user. As I can see everything is setup correctly, I can select days, save that data to the db and on editing I can retrieve data back to the ui, but problem is that this data is correctly loaded on edit mode inside ui but it's not selected on week days ui control.

1 picture is saying more that 1000 words :)

on load data (edit mode) this is current situation enter image description here

but should be like this enter image description here

this input with id DaysOfWeekSelected once setup correctly will be hidden by default.

Knowing all this and having data already inside view how can select this days from index (0,5,6) into plugin?

Just to mention that all scripts are loaded correctly, plugin works correctly on user selection (change days).

Update: I tried with some dummy data select days on page load using

$("#repeatOnDays").weekLine("setSelected", "0,1,2,3,4");

anyone to help?

Asp.net MVC Controller Refresh add same value to session everytime

I am trying to create a shopping cart in asp.net MVC. I write the following code in my controller to get the select item from the product list to my cart.

public ActionResult OrderNow(int id) { decimal? total;

        if (Session["cart"] == null)
        {
            List<Item> cart = new List<Item>();
            cart.Add(new Item(db.Products.Find(id),1));
              foreach(var result in cart)
              {
                  //ViewBag.price = result.Pr.UnitPrice * 1;
                  total = cart.Sum(item => item.Pr.UnitPrice);
                  ViewBag.price = total;
                  Session["total"] = total;
                  result.Quantity = 1;
              }
            Session["cart"] = cart;
        }
        else
        {
            List<Item> cart = (List<Item>)Session["cart"];
            for (int i = 0; i < cart.Count; i++)
                if (cart[i].Pr.ProductID == id)
                {
                    cart[i].Quantity++;
                    total = cart.Sum(item => item.Pr.UnitPrice);
                    ViewBag.price = total;
                    Session["total"] = total;
                }

                else
                {
                    cart.Add(new Item(db.Products.Find(id), 1));
                    total = cart.Sum(item => item.Pr.UnitPrice);
                    ViewBag.price = total;
                    Session["total"] = total;
                }

            //cart.Add(new Item(db.Products.Find(id), 1));
            Session["cart"] = cart;

        }
        return View("Cart");
    }

here is the view for my shopping cart

<table class="table table-condensed">
                <thead>
                    <tr class="cart_menu">
                        <td class="image">Item</td>
                        <td class="description"></td>
                        <td class="price">Price</td>
                        <td class="quantity">Quantity</td>
                        <td class="total">Total</td>
                        <td></td>
                    </tr>
                </thead>
                <tbody>
                    @foreach(Item item in (List<Item>)Session["cart"])
                    { 
                    <tr>
                        <td class="cart_product">
                            <img src="@Url.Content(item.Pr.Picture1)" alt="" width="75px" height="75px"  />
                        </td>
                        <td class="cart_description">
                            <h4><a href="">@item.Pr.ProductName</a></h4>
                            <p>Product ID: @item.Pr.ProductID</p>
                        </td>
                        <td class="cart_price">
                            <p>$@item.Pr.UnitPrice</p>
                        </td>
                        <td class="cart_quantity">
                            <div class="cart_quantity_button">
                                @*<a class="cart_quantity_up" href=""> + </a>*@
                                @Html.TextBoxFor(Model => item.Quantity , new { @class = "cart_quantity_input", @size = "2" })
                                @*<input class="cart_quantity_input" type="text" name="quantity" value="1" autocomplete="off" >*@
                                @*<a class="cart_quantity_down" href=""> - </a>*@
                            </div>
                        </td>
                        <td class="cart_total">
                            <p class="cart_total_price">$@(item.Pr.UnitPrice * item.Quantity)</p>
                        </td>
                        <td class="cart_delete">
                            <a class="cart_quantity_delete" href=""><i class="fa fa-times"></i></a>
                        </td>
                    </tr>
                    }
                </tbody>
            </table>

but I am getting an issue in this code whenever I refresh the page its add one more item in my cart and if I select the one product its add the product next row not add count to items..

I hope you understand what I want to ask.. Anybody can resolve the issue ?

How to use session with @html.displayfor

Please I need help on how to get the generated I.d from a view using session in mvc...

@Html.Displayfor(m => m._consultation.consultid) 

Assuming the value of the Consultid is 1, I want to use session to get that I.D

Strange behaviour when using SqlDataReader, StreamWriter and FileStream in MVC application vs Console application

I created a method (OutputFile) that takes in data using SQlDataReader and then outputs it to a local file using StreamWriter. To test that the method output the correct data I created a console application and when the function is called from this it takes roughly 4 minutes which was expected as the stored procedure involved brings back a lot of records (~200,000).

   public void OutputFile(string query, string path, params object[] parameters)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["Database"].ConnectionString;

        using (var connection = new SqlConnection(connectionString))
        {
            using (SqlCommand command = new SqlCommand(query, connection))
            {
                command.CommandType = System.Data.CommandType.StoredProcedure;

                // Open connection
                con.Open();

                Debug.WriteLine("Starting FileStream");
                using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
                {
                    Debug.WriteLine("Execute Reader");
                    using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess))
                    {
                        using (StreamWriter writer = new StreamWriter(fileStream))
                        {
                            Debug.WriteLine("Writing Files");
                            while (reader.Read())
                            {
                                char[] buffer = new char[4096];
                                int charsRead = 0;
                                using (TextReader data = reader.GetTextReader(0))
                                {
                                    charsRead = data.Read(buffer, 0, buffer.Length);
                                    if (charsRead > 0)
                                    {
                                        writer.WriteLine(buffer, 0, charsRead);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

When I moved the method into my MVC application the execution time increased greatly. The first time I ran the application it took 14 minutes to output the file and the second time it took 30 minutes. I have included the output from the debug windows below.

When running the application I made sure to turn check thrown CLR exceptions, but none were thrown.

Thanks in for advance for any help that can explain why this issue is occurring.

Additional notes about the MVC application (the console application did not include these references)

  • Uses Entity Framework and Unity Framework
  • Uses Glimpse

Debug output from the file that is produced in 14 minutes

Starting FileStream
Execute Reader
The thread 0x56c has exited with code 259 (0x103).
The thread 0x1194 has exited with code 259 (0x103).
The thread 0x568 has exited with code 0 (0x0).
The thread 0x13f8 has exited with code 259 (0x103).
The thread 0xb6c has exited with code 259 (0x103).
Writing Files
The thread 0xfa8 has exited with code 259 (0x103).
The thread 0x136c has exited with code 259 (0x103).

Debug output from the file that is produced in 30 minutes

Starting FileStream
Execute Reader
The thread 0x1290 has exited with code 259 (0x103).
The thread 0x1030 has exited with code 259 (0x103).
The thread 0x11cc has exited with code 259 (0x103).
The thread 0x1310 has exited with code 259 (0x103).
The thread 0x1d4 has exited with code 259 (0x103).
The thread 0xbb8 has exited with code 259 (0x103).
The thread 0x179c has exited with code 259 (0x103).
The thread 0x1390 has exited with code 259 (0x103).
The thread 0x169c has exited with code 259 (0x103).
The thread 0x16a0 has exited with code 259 (0x103).
The thread 0x1424 has exited with code 259 (0x103).
The thread 0x16e8 has exited with code 259 (0x103).
The thread 0x14f0 has exited with code 259 (0x103).
The thread 0x1458 has exited with code 259 (0x103).
The thread 0x17f8 has exited with code 259 (0x103).
The thread 0x1094 has exited with code 259 (0x103).
The thread 0x1484 has exited with code 259 (0x103).
The thread 0x1540 has exited with code 259 (0x103).
The thread 0x1694 has exited with code 259 (0x103).
The thread 0x17c8 has exited with code 259 (0x103).
The thread 0x1660 has exited with code 259 (0x103).
The thread 0x1764 has exited with code 259 (0x103).
The thread 0x1548 has exited with code 259 (0x103).
The thread 0x172c has exited with code 259 (0x103).
The thread 0x17d8 has exited with code 259 (0x103).
The thread 0x1674 has exited with code 259 (0x103).
The thread 0x1380 has exited with code 259 (0x103).
Writing Files
The thread 0x16a8 has exited with code 259 (0x103).

How use viewModels + Entity Framework? Is necessary add a migration?

I am using asp.net mvc 4 + entity framework and for the first time I'm using ViewModels. I'm reading this tutorial: http://ift.tt/1DYBwYe

After step in that it creates classes viewmodel, he's going to create the controller, but does not explain whether to create a migration to add these classes in the bank, it is necessary?

For I'm creating a migration like this:

add-migration xxx

But when the migration is created, the methods of up () and down () are coming empty, can anyone help me?

center image in thumbnail

This is what I have:
enter image description here
And I want that low res images will be centered like this: (edited with paint)
enter image description here

This is the razor view:

<div>
<div class="row">
    @foreach (var item in Model)
    {
        <div class="col-md-3 ">
            <div class="thumbnail centerThumb" style="height:345px; width:280px;margin-bottom:15px">
                <div style="height:125px; ">
                    <a href="@Url.Action("Details", "Items", new { id = item.ItemID})">
                        <img src="~/Content/thumbnails/@item.Img" alt="thmbnail" width="255" class="img-rounded centerThumb">
                    </a>
                </div>
                <div class="caption">
                    <h3>@Html.DisplayFor(modelItem => item.Name)</h3>
                    <p style="height:95px">@Html.DisplayFor(modelItem => item.Description)</p>
                    <p style="position:fixed">
                        <a href="#" class="btn btn-primary" role="button">Add to Cart</a>
                    </p>
                    <p style="margin-left:50%"> @Html.DisplayFor(modelItem => item.Price) </p>
                </div>
            </div>
        </div>
    }
</div>

JQuery Mobile ajax enabled causing client side validation not working

I am learning the JQuery Mobile framewrok and is using VS 2013 for my new project. I created a MVC 4 JQuery Mobile project. From the project I got out of the box, I click on the Login link and on that login page, I click on the Login Button, it shows the expected error messages. I want my project to be ajax enabled so I set the $.mobile.ajaxEnabled = true; and debug in VS, it hits the break point I set in the Login action in the AccountController so the client-side validation didn't work and got to the post on the server-side.

Is it by design that I can't use the built-in client-side validation when ajaxEnabled is set to true? Or is there some settings that I need to set? I need my project to be jax enabled and would like to use the built-in client side validation if possible and don't have to write custom jquery validation code.